How to specify jackson to only use fields - preferably globally

前端 未结 8 679
旧巷少年郎
旧巷少年郎 2020-11-22 17:03

Default jackon behaviour seems to use both properties (getters and setters) and fields to serialize and deserialize to json.

I would like to use the fields as the ca

8条回答
  •  余生分开走
    2020-11-22 17:57

    In Jackson 2.0 and later you can simply use:

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;   
    
    ...
    
    ObjectMapper mapper = new ObjectMapper();    
    mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    

    to turn off autodetection.

提交回复
热议问题