简体   繁体   中英

Jackson ignores JsonProperty annotation in deserialization

I've got a bit of a conundrum. I'm trying to deserialize a json message into a pojo using the builder pattern and I'm getting the following error:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Information" 
(class com.verification.client.models.response.Response$Builder), not marked as ignorable (3 known properties: "status", "products", "information"])

This is very confusing to me as I've clearly marked the field in the pojo with a JsonProperty annotation:

@JsonDeserialize(builder = Response.Builder.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Response {
    @JsonProperty("Status")
    private final Optional<Status> status;
    @JsonProperty("Products")
    private final Optional<List<ResponseProduct>> products;
    @JsonProperty("Information")  //Here's where the field is defined
    private final Optional<List<ResponseInformation>> information;

    private Response(final Builder b){
        this.status = b.status;
        this.products = b.products;
        this.information = b.information;
    }

    public Optional<Status> getStatus() {
        return status;
    }

    public Optional<List<ResponseProduct>> getProducts() {
        return products;
    }

    public Optional<List<ResponseInformation>> getInformation() {
        return information;
    }

    @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
    public static class Builder{
        private Optional<Status> status;
        private Optional<List<ResponseProduct>> products = Optional.empty();
        private Optional<List<ResponseInformation>> information = Optional.empty();

        public Builder(){}

        public Builder status(final Status status){
            this.status = Optional.of(status);
            return this;
        }

        public Builder products(final List<ResponseProduct> products){
            this.products = Optional.of(products);
            return this;
        }

        public Builder information(final List<ResponseInformation> information){
            this.information = Optional.of(information);
            return this;
        }

        public Response build(){
            return new Response(this);
        }
    }
}

I have a feeling it's something small, but at this point I am at a loss for why my code is behaving this way.

PS Here's the json I'm deserializing

{
"Information": [{
    "InformationType": "error-details",
    "Code": "internal_application_error",
    "Description": "Error: Internal",
    "DetailDescription": []
}]
}

Solved this a while back, answering for posterity.

The issue I was having is that the build methods were not being correctly interpreted, but by defining @JsonSetter annotations on the methods of the static build class Jackson was able to correctly interpret the input and build the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM