我有点难题。我尝试使用构建器模式将json消息反序列化为pojo,并且我收到以下错误:
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"])
这对我来说非常混乱,因为我已经使用JsonProperty注释清楚标记了pojo中的字段:
@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);
}
}
}
我觉得它有点小,但是在这一点上我无法理解为什么我的代码会这样做。
P.S。 这是我要反序列化的json
{
"Information": [{
"InformationType": "error-details",
"Code": "internal_application_error",
"Description": "Error: Internal",
"DetailDescription": []
}]
}
答案 0 :(得分:0)
一会儿解决了这个问题,为后代回答。
我遇到的问题是构建方法没有被正确解释,但是通过在静态构建类的方法上定义@JsonSetter注释,Jackson能够正确地解释输入并构建对象。