16

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?

Rands7
  • 161
  • 4
  • I didn't get the first approach to the original problem working, so I resorted to the second and discovered that (of course) jackson annotations on fields don't carry over to builder methods, so I am interested in what comes up. – simon Mar 28 '18 at 11:49
  • @simon posted an answer that worked for me. just want to confirm the version that you were using when you experienced this issue – shinjw May 27 '18 at 08:28
  • You can use Lombok with immutability + Jackson: [answer](https://stackoverflow.com/a/55957865/6629515) – Oleksandr Yefymov May 02 '19 at 18:00
  • You can use lombok builder and jackson together: https://stackoverflow.com/a/48801237/4944734 – ganesan dharmalingam Jul 22 '20 at 10:41

2 Answers2

2

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;
        }  
    }
}
kakabali
  • 3,824
  • 2
  • 29
  • 58
simon
  • 707
  • 8
  • 22
0

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.

shinjw
  • 3,329
  • 3
  • 21
  • 42