![](/img/trans.png)
[英]How to ignore certain fields for deserialization without jackson annotations?
[英]Ignore some fields deserialization with jackson without changing model
我正在寻找一种配置杰克逊解串器以忽略某些字段的方法。 我不想通过注释模型来实现这一点,因为它是由另一个项目给出的。 我只是想通过构造解串器(ObjectMapper)来做到这一点。
可能吗?
你必须做以下
1)编写自己的Deserializer,以扩展JsonDeserializer
2)覆盖反序列化方法并在忽略某些字段后返回您的类对象
3)向ObjectMapper注册反序列化器
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(yourClass.class, new yourDerializer());
mapper.registerModule(module);
String newJsonString = "{\"id\":1}";
final yourClass yourClassObject= mapper.readValue(newJsonString, yourClass.class);
希望这能解决您的问题
您可以使用Mix-In
注释来实现。
class ThirdPartyReadOnlyClass {
private String ignoredPropertyFromThirdParty;
public String getIgnoredPropertyFromThirdParty() {
return ignoredPropertyFromThirdParty;
}
}
abstract class MixIn {
@JsonIgnore
String getIgnoredPropertyFromThirdParty();
}
您可以将JSON注释放在MixIn类上,就像将它们放在原始模型类上一样。
配置对象映射器
objectMapper.addMixInAnnotations(ThirdPartyReadOnlyClass.class, MixIn.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.