Lombok 1.18.0 and Jackson 2.9.6 not working together

后端 未结 5 983
南笙
南笙 2020-12-09 03:03

The deserialization is failing after the update.

I updated my micro-service from Spring 1.5.10.RELEASE to Spring 2.0.3.RELEASE

5条回答
  •  佛祖请我去吃肉
    2020-12-09 03:47

    You want to deserialize a class which has final field. so u need to declare a constructor which contains final field to deserialize.

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class ErrorDetail {
    
    private final String heading;
    private final String detail;
    private String type;
    
    @JsonCreator
    public ErrorDetail(@JsonProperty("heading") String heading, @JsonProperty("detail") String detail) {
        this.heading = heading;
        this.detail = detail;
    }
    }
    

    and when deserialize with mapper need to MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS set this property false.

    private static  T asObject(final String str, Class clazz) {
        try {
            return new ObjectMapper().configure(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS,false).readValue(str, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    

提交回复
热议问题