JavaSheriff
JavaSheriff

Reputation: 7687

UnrecognizedPropertyException: Unrecognized field - jackson 2.9

I am doing a simple conversion using Jackson:

response = mapper.readValue(responseStr, PrinterStatus.class);

The code is throwing this exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "OutputParameters" (class com.xerox.PrinterStatus), 
not marked as ignorable (one known property: "outputParameters"]) at ....  
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)

The Json I would like to convert is very simple:

{
    "OutputParameters": {
        "@xmlns": "http://xmlns.xerox.com/apps/rest/",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "GETPRINTERSTATUS": {
            "GETPRINTERSTATUS_ITEM": [{
                "STATUS": "True",
                "MESSAGE": " "
            }]
        }
    }
}

This is the PrinterStatus class, it has the field "OutputParameters"
So I am not sure what is Jackson yelling about.

public class PrinterStatus {

private OutputParameters outputParameters;

public OutputParameters getOutputParameters() {
    return outputParameters;
}

public void setOutputParameters(OutputParameters outputParameters) {
    this.outputParameters = outputParameters;
}

...

Upvotes: 0

Views: 10489

Answers (1)

zforgo
zforgo

Reputation: 3316

Basically JSON keys are case sensitive. Accordingly OutputParameters doesn't equal to outputParameters.

So you have to choose:

  1. rename the field in Java class (and getters / setters too) to OutputParameters
  2. rename JSON property key to outputParameters
  3. If you using Jackson 2.9 or above just simply annotate field like this:
public class PrinterStatus {

    @JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
    private OutputParameters outputParameters;

    public OutputParameters getOutputParameters() {
        return outputParameters;
    }

    public void setOutputParameters(OutputParameters outputParameters) {
        this.outputParameters = outputParameters;
    }
...
}
  1. Set property name explicitly
public class PrinterStatus {

    @JsonProperty("OutputParameters")
    private OutputParameters outputParameters;
...
}
  1. Enable case insesitive feature globally
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

Upvotes: 4

Related Questions