Using lombok @Singular with jackson @JsonPOJOBuilder

After debugging through Jackson's smile deserializer, I found that mine List, which was annotated with help @Singular, was not found by Jackson.

Is there a way to do it @Singularwith jackson @JsonPOJOBuilder?

@JsonDeserialize(builder = MyClass.MyClassBuilder.class)
@Value
@Builder
@RequiredArgsConstructor
@EqualsAndHashCode
public class MyClass {

    @NonNull String name;
    @NonNull @Singular List<String> favs = new ArrayList<>();

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

changing @Singularto @Builder.Defaultworks fine.

+4
source share
1 answer

The problem is that you are initializing the field favs:

List<String> favs = new ArrayList<>();

What because of @ Meaning annotation final. Therefore, it cannot be set / overwritten by the constructor or constructor generated by @RequiredArgsConstructor . Check by trying to create an instance yourself:

MyClass.builder().name("a").fav("1").fav("2").build();

intellij fav, @Builder @Singular .
:

  • favs lombok, jackson initialize
  • ,

lombok 1.16.18 jackson 2.9.2

+1

Source: https://habr.com/ru/post/1688318/


All Articles