How to specify jackson to only use fields - preferably globally

前端 未结 8 544
旧巷少年郎
旧巷少年郎 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:44

    You can configure individual ObjectMappers like this:

    ObjectMapper mapper  = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                    .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                    .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    

    If you want it set globally, I usually access a configured mapper through a wrapper class.

提交回复
热议问题