Home > Blockchain >  Unrecognized field during deserialization despite @SerializedName annotation
Unrecognized field during deserialization despite @SerializedName annotation

Time:02-11

I am fetching some data via a REST service, but I am getting this error when deserializing the response :

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResultSet Output" (class com.HolderCollectionWrapper), not marked as ignorable (one known property: "holders"]) at [Source: java.io.ByteArrayInputStream@74efa7bd; line: 1, column: 22] (through reference chain: com.HolderCollectionWrapper["ResultSet Output"])

This is my code :

response = restTemplate.exchange(requestUrl, HttpMethod.GET, request, HolderCollectionWrapper.class);


public class HolderCollectionWrapper {

    @SerializedName("ResultSet Output")
    private List<Holder> holders;

    public List<Holder> getHolders() {
        return holders;
    }

    public void setHolders(List<Holder> holders) {
        this.holders = holders;
    }
}

This is the JSON I am getting :

{
    "ResultSet Output": [
        {...}, {...}, {...}
    ]
}

Despite the @SerializedName("ResultSet Output"), it's not working, why ?

CodePudding user response:

@SerializedName is a gson annotation and you are using jackson library for serialization.

The jackson annotation for field name is @JsonProperty

Try:

@JsonProperty("ResultSet Output")
private List<Holder> holders;

CodePudding user response:

This happens because the SerializedName("ResultSet Output") gson annotation indicates that the holders will be serialized with the ResultSet Output name like the json example you post; to deserialize it with jackson you have to use the JsonProperty annotation, specifying the ResultSet Output name applied on the setter to avoid possible conflicts with the gson library used for serialization:

public class HolderCollectionWrapper {

    @SerializedName("ResultSet Output")
    private List<Holder> holders;

    public List<Holder> getHolders() {
        return holders;
    }

    @JsonProperty("ResultSet Output")
    public void setHolders(List<Holder> holders) {
        this.holders = holders;
    }
}
  • Related