Jackson deserializer - change null collection to empty one

前端 未结 3 685
生来不讨喜
生来不讨喜 2020-12-18 18:30

I have an entity that contains collection as attribute:

public class Entity {

    @JsonProperty(value=\"homes\")
    @JsonDeserialize(as=HashSet.class, cont         


        
3条回答
  •  暖寄归人
    2020-12-18 19:13

    What worked for me was simply to remove the setter and make the attribute final. jackson 2 will then use the getter to modify the list.

    public class Entity {
    
      @JsonProperty(value="homes")
      @JsonDeserialize(as=HashSet.class, contentAs=HomeImpl.class)
      private final Collection homes = new ArrayList();
    
      public List getHomes() {
         return homes;
      }
    }
    

    The responsible feature is USE_GETTERS_AS_SETTERS which is turned on by default: https://github.com/FasterXML/jackson-databind/wiki/Mapper-Features

提交回复
热议问题