Lombok + Jackson immutables

空扰寡人 提交于 2020-07-31 07:33:45

问题


After updating my project to Spring Boot 1.5.10 Lombok stopped working correctly with Jackson. I mean immutable DTOs creation, when field names in my objects are not same as fields in json request:

@Value
@Builder
public class MyImmutableDto implements Serializable {

    @JsonProperty("other-field-1-name")
    private final BigDecimal myField1;

    @JsonProperty("other-field-2-name")
    private final String myField2;

    and a lot of fields there...
}

So, after updating Spring Boot to 1.5.10 this code isn't working, and I need to configure lombok like that:

lombok.anyConstructor.addConstructorProperties = true

Does anyone know any other way to create such objects with jackson + lombok without this lombok fix? Instead of this fix I can use following code: @JsonPOJOBuilder and @JsonDeserialize(builder = MyDto.MyDtoBuilder.class):

@Value
@Builder
@JsonDeserialize(builder = MyDto.MyDtoBuilder.class)
public class MyDto implements Serializable {

    // @JsonProperty("other-field-1-name")    // not working
    private final BigDecimal myField1;

    private final String myField2;
    private final String myField3;
    and a lot of fields there...

    @JsonPOJOBuilder(withPrefix = "")
    public static final class MyDtoBuilder {
    }
}

But it is not working with @JsonProperty("other-field-1-name"). Ofc, it can be done by simple @JsonCreator, but maybe there is some way to use it with lombok using some constructor/jackson annotations?


回答1:


So this is not the exact same case, but this works for my problem. I need the @JsonDeserialize annotation on the builder, putting it there on the builder explicitly solves the problem (at the cost of boilerplate code). At least I don't need to type out the rest of the builder.

@Value
@Builder
@JsonDeserialize(builder = ProductPrice.ProductPriceBuilder.class)
public class ProductPrice {

    @JsonSerialize(using = MoneySerializer.class)
    @JsonDeserialize(using = MoneyDeserializer.class)
    Money price;

    Duration rentalLength;

    Period recurrence;

    @JsonPOJOBuilder(withPrefix = "")
    public static class ProductPriceBuilder{
        @JsonDeserialize(using = MoneyDeserializer.class)
        public ProductPrice.ProductPriceBuilder price(Money price) {
            this.price = price;
            return this;
        }  
    }
}



回答2:


Considering that this question was asked after Jan 2017, I'm assuming that you may have upgraded your version of Lombok 1.16.20along with Spring Boot. And still using JDK 8

You can update your version of Spring Boot, but may want to keep your Lombok version at 1.16.18. This will let you implement your extra customization and deserialization via builder work around. That is, as long as you haven't made use of any new lombok annotations.

There was a good amount of effort in 1.16.20 specifically that addresses breaking changes in JDK 9 which can potentially cause issues moving forward on JDK 8.

1.16.20 @Data object no longer constructable for jackson? < Other individuals who recognized similar issues.



来源:https://stackoverflow.com/questions/48644809/lombok-jackson-immutables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!