Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson UnrecognizedPropertyException

I am trying to convert JsonString to Java object.

Json object

{
"action": "added",
"data": {
    "Quote": {
        "TotalDiscountsAmount": 0,
        "Id": "test123"
    },
    "Owner": {
        "Username": "00000000",
        "Id": "00000000"
    },
    "Discount_Amount__c": 0,
    "Base_List_Price__c": 574.88,
    "TotalList": 574.88,
    "Id": "000000",
    "ExtendedTotalList": 574.88,
    "BaseListPrice": 474.88
}
}

My POJOs:

AddtocartJson.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "action",
  "data"
})
public class AddToCartJson {
    @JsonProperty("action")
    private String action;
    @JsonProperty("data")
    private Data data;

    @JsonProperty("action")
    public String getAction() {
        return action;
    }

    @JsonProperty("action")
    public void setAction(String action) {
        this.action = action;
    }

    @JsonProperty("data")
    public Data getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(Data data) {
        this.data = data;
    }
}

Data.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "Quote",
  "Owner",
  "Discount_Amount__c",
  "Base_List_Price__c",
  "TotalList",
  "Id",
  "ExtendedTotalList",
  "BaseListPrice"
})
public class Data {

    @JsonProperty("Quote")
    private com.product.json.Quote Quote;
    @JsonProperty("Owner")
    private com.product.json.Owner Owner;
    @JsonProperty("Discount_Amount__c")
    private Integer DiscountAmountC;
    @JsonProperty("Base_List_Price__c")
    private Double BaseListPriceC;
    @JsonProperty("TotalList")
    private Double TotalList;
    @JsonProperty("Id")
    private String Id;
    @JsonProperty("ExtendedTotalList")
    private Double ExtendedTotalList;
    @JsonProperty("BaseListPrice")
    private Double BaseListPrice;

    @JsonProperty("Quote")
    public com.product.json.Quote getQuote() {
        return Quote;
    }

    @JsonProperty("Quote")
    public void setQuote(com.product.json.Quote Quote) {
        this.Quote = Quote;
    }

    @JsonProperty("Owner")
    public com.product.json.Owner getOwner() {
        return Owner;
    }

    @JsonProperty("Owner")
    public void setOwner(com.product.json.Owner Owner) {
        this.Owner = Owner;
    }

    @JsonProperty("Discount_Amount__c")
    public Integer getDiscountAmountC() {
        return DiscountAmountC;
    }

    @JsonProperty("Discount_Amount__c")
    public void setDiscountAmountC(Integer DiscountAmountC) {
        this.DiscountAmountC = DiscountAmountC;
    }

    @JsonProperty("Base_List_Price__c")
    public Double getBaseListPriceC() {
        return BaseListPriceC;
    }

    @JsonProperty("Base_List_Price__c")
    public void setBaseListPriceC(Double BaseListPriceC) {
        this.BaseListPriceC = BaseListPriceC;
    }

    @JsonProperty("TotalList")
    public Double getTotalList() {
        return TotalList;
    }

    @JsonProperty("TotalList")
    public void setTotalList(Double TotalList) {
        this.TotalList = TotalList;
    }

    @JsonProperty("Id")
    public String getId() {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id) {
        this.Id = Id;
    }

    @JsonProperty("ExtendedTotalList")
    public Double getExtendedTotalList() {
        return ExtendedTotalList;
    }

    @JsonProperty("ExtendedTotalList")
    public void setExtendedTotalList(Double ExtendedTotalList) {
        this.ExtendedTotalList = ExtendedTotalList;
    }

    @JsonProperty("BaseListPrice")
    public Double getBaseListPrice() {
        return BaseListPrice;
    }

    @JsonProperty("BaseListPrice")
    public void setBaseListPrice(Double BaseListPrice) {
        this.BaseListPrice = BaseListPrice;
    }
}

Quote.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "TotalDiscountsAmount",
  "Id"
})
public class Quote {
    @JsonProperty("TotalDiscountsAmount")
    private Integer TotalDiscountsAmount;
    @JsonProperty("Id")
    private String Id;

    @JsonProperty("TotalDiscountsAmount")
    public Integer getTotalDiscountsAmount() {
        return TotalDiscountsAmount;
    }

    @JsonProperty("TotalDiscountsAmount")
    public void setTotalDiscountsAmount(Integer TotalDiscountsAmount) {
        this.TotalDiscountsAmount = TotalDiscountsAmount;
    }

    @JsonProperty("Id")
    public String getId() {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id) {
        this.Id = Id;
    }
}

When I try to convert JSON to Java class using the mapper, it's throwing an UnrecognizedPropertyException. Seems likes even though I have a Quote object somehow mapper is not able to recognize the object.

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Quote" (Class com.product.json.Data), not marked as ignorable at [Source: java.io.StringReader@11547748; line: 1, column: 36] (through reference chain: com.product.json.AddToCartJson["data"]-> com.product.json.Data["Quote"]) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267) at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)

How can I fix this?

like image 386
sree Avatar asked Oct 13 '15 12:10

sree


People also ask

How do I ignore unrecognized field Jackson?

Alternatively, you can also use @JsonIgnoreProperties annotation to ignore undeclared properties. The @JsonIgnoreProperties is a class-level annotation in Jackson and it will ignore every property you haven't defined in your POJO.

What is UnrecognizedPropertyException?

Class UnrecognizedPropertyExceptionSpecialized JsonMappingException sub-class specifically used to indicate problems due to encountering a JSON property that could not be mapped to an Object property (via getter, constructor argument or field).

How do I ignore JSON fields?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

What is JSON processing exception?

Class JsonProcessingExceptionIntermediate base class for all problems encountered when processing (parsing, generating) JSON content that are not pure I/O problems. Regular IOException s will be passed through as is. Sub-class of IOException for convenience.


2 Answers

The problem here is that OP mixes two different Jackson versions:

  • 1.9 from org.codehaus (org.codehaus.jackson.map.exc.UnrecognizedPropertyException shows that)
  • 2.x from com.fasterxml (visible in the imports import com.fasterxml.jackson.annotation.)

As pointed out by sam in the comments, OP should only use one version. Either use 2.x for deserialization or use annotations from the old 1.9 version. But it is recommended to use the newest version from com.fasterxml, instead of the old one.

like image 110
2 revs Avatar answered Nov 10 '22 11:11

2 revs


you might want to ignore undefined properties by using Jackson's class-level annotation:

@JsonIgnoreProperties

like image 35
Oleksii Kyslytsyn Avatar answered Nov 10 '22 12:11

Oleksii Kyslytsyn