Jersey/Jackson @JsonIgnore on setter

后端 未结 5 1101
暗喜
暗喜 2021-02-13 18:28

i have an class with the following annotations:

class A {
public Map> references;

@JsonProperty
public Map

        
5条回答
  •  粉色の甜心
    2021-02-13 19:08

    I think there are two key pieces that should enable you to have "read-only collections" as desired. First, in addition to ignoring the setter, ensure that your field is also marked with @JsonIgnore:

    class A {
    
      @JsonIgnore
      public Map> references;
    
      @JsonProperty
      public Map> getReferences() { ... }
    
      @JsonIgnore
      public void setReferences(Map>) { ... }
    
    }
    

    Second, in order to prevent the getters from being used as setters, disable the USE_GETTERS_AS_SETTERS feature:

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
    

提交回复
热议问题