Can't make Jackson and Lombok work together

后端 未结 14 1383
伪装坚强ぢ
伪装坚强ぢ 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:22

    I struggled with this for a moment as well. But looking through the documentation here I can see that the onConstructor annotation parameter is considered experimental and is not supported well on my IDE (STS 4). According to the Jackson documentation, private members are not (de)serialized by default. There are quick ways to resolve this.

    Add JsonAutoDetect annotation and set it appropriately to detect protected/private members. This is convenient for DTOs

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
    public class SomeClass
    

    Add a factory function with @JsonCreator annotation, this works best if you need some object validation or additional transforms.

    public class SomeClass {
    
       // some code here
    
       @JsonCreator
       public static SomeClass factory(/* params here dressing them in @JsonProperty annotations*/) {
          return new SomeClass();
       }
    }
    

    Of course you could just manually add the constructor in yourself also as well.

提交回复
热议问题