配置ObjectMapper以在反序列化时使用字段而不是Setter

时间:2016-12-19 09:45:09

标签: java json jackson

Java模型:

@lombok.Data
public class Foo {
  ...
  private boolean isDefault;
}

序列化为JSON:

{
  ...,
  "isDefault" : true
}

ObjectMapper配置:

ObjectMapper mapper = new ObjectMapper();
VisibilityChecker<?> vc = objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
    .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
    .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
    .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
    .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
    .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(vc);

问题:代码无法将JSON反序列化为java对象,并且UnrecognizedPropertyException的{​​{1}}失败,即使它完全将其序列化为JSON。有什么想法?
提前谢谢。

0 个答案:

没有答案