在使用Jackson ObjectMapper时如何忽略pojo注释? [英] How to ignore pojo annotations while using Jackson ObjectMapper?
问题描述
我有一个带有Jackson批注的POJO
I've a POJO with Jackson annotations
public class Sample{
private String property1;
@JsonIgnore
private String property2;
//...setters getters
}
因此,当其他框架(例如RestEasy)将Jackson库用于自动编组时,这些注释将帮助指导序列化和反序列化过程.
So, when Jackson library is used for automarshalling by other frameworks such as RestEasy these annotations help guide the serialization and deserilization process.
但是,当我想使用ObjectMapper mapper = new ObjectMapper()显式进行序列化时,我不希望这些注释产生任何效果,而是根据自己的需求配置mapper对象.
But when I want to explicitly serialize using ObjectMapper mapper = new ObjectMapper(), I don't want those annotations to make any effect, instead I will configure the mapper object to my requirement.
那么,如何在使用ObjectMapper时使注释不起作用?
So, how to make the annotations not make any effect while using ObjectMapper?
推荐答案
配置您的ObjectMapper
不要使用这些注释,如下所示:
Configure your ObjectMapper
not to use those Annotations, like this:
ObjectMapper objectMapper = new ObjectMapper().configure(
org.codehaus.jackson.map.DeserializationConfig.Feature.USE_ANNOTATIONS, false)
.configure(org.codehaus.jackson.map.SerializationConfig.Feature.USE_ANNOTATIONS, false);
这行得通!
这篇关于在使用Jackson ObjectMapper时如何忽略pojo注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!