【问题标题】:Getting JsonIgnore or JsonIgnoreProperties to work on servlet filter让 JsonIgnore 或 JsonIgnoreProperties 在 servlet 过滤器上工作
【发布时间】:2012-11-28 06:33:54
【问题描述】:

必须在 servlet 过滤器中将 Java 对象序列化为 json 字符串以用于 jersey 应用程序。

有以下对象,

 @XmlRootElement
 @JsonIgnoreProperties({"bar"})
 public class Foo{
    String a="1";

    String b="2";

    @JsonIgnore
    String bar="3";

    };



      ObjectMapper om = new ObjectMapper();
      om.writeValueAsString(fooObject);

返回,

        {
          a:"1",
          b:"2",
          bar:"3" // Inspite of Jsonignore and jsonignoreproperties bar is returned
          }

我该如何克服这个问题?

【问题讨论】:

    标签: java json jersey jackson


    【解决方案1】:

    修复从 codehaus 到 fastxml 的导入并将以下功能添加到映射器修复了该问题。

          mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
    

    【讨论】:

      【解决方案2】:

      使用@XmlTransient:

      @XmlTransient
      String bar="3";
      

      【讨论】:

        猜你喜欢
        • 2015-07-08
        • 2017-11-25
        • 2021-03-28
        • 2015-04-02
        • 2013-08-16
        • 2015-02-26
        相关资源
        最近更新 更多