Can't make Jackson and Lombok work together

后端 未结 14 1416
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 13:48

I am experimenting in combining Jackson and Lombok. Those are my classes:

package testelombok;

import com.fasterxml         


        
14条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 14:30

    From Jan Rieke's Answer

    Since lombok 1.18.4, you can configure what annotations are copied to the constructor parameters. Insert this into your lombok.config:

    lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonProperty
    

    Then just add @JsonProperty to your fields:

    ...

    You'll need a @JsonProperty on every field even if the name matches, but that is a good practice to follow anyway. You can also set your fields to public final using this, which I prefer over getters.

    @ToString
    @EqualsAndHashCode
    @Wither
    @AllArgsConstructor(onConstructor=@__(@JsonCreator))
    public class TestFoo {
        @JsonProperty("xoom")
        public final String x;
        @JsonProperty("z")
        public final int z;
    }
    

    It should also work with getters (+setters) though.

提交回复
热议问题