Json仅在特定方法中忽略属性 [英] Json Ignore property only in specific method

查看:275
本文介绍了Json仅在特定方法中忽略属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个课,如下所示:

I have a Class So as shown below:

@Entity
@Table(name = "so", schema = "public")
public class So implements java.io.Serializable , IBusiness {

    private int idSo;

    private Set<PartOrder> partOrders = new HashSet<PartOrder>();

    public So() {

    }

    @Id
    @SequenceGenerator(name = "seqGenerator", sequenceName = "so_seq", allocationSize=1, initialValue=1)
    @GeneratedValue(generator="seqGenerator", strategy=GenerationType.SEQUENCE)
    @Column(name = "id_so", unique = true, nullable = false)
    public int getIdSo() {
        return this.idSo;
    }

    public void setIdSo(int idSo) {
        this.idSo = idSo;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "so")
    public Set<PartOrder> getPartOrders() {
        return this.partOrders;
    }

    public void setPartOrders(Set<PartOrder> partOrders) {
        this.partOrders = partOrders;
    }


}

我有一个返回SO的Json的方法.但我不希望partOrders属性包含在响应json中.我在属性上使用@JsonIgnore,但是现在,我有另一种接收SO对象的方法,我需要获取partOders.

I have a method that returns a Json with the SO. But i don not want the partOrders attribute to be included on the response json. I was using @JsonIgnore on the attribute but now, i have another method that receives an SO object and i need to get the partOders.

问题是:有什么方法可以指定何时忽略属性(在特定方法中)?

The question is: Is there any way to specify when to ignore the attribute (in specific method)?

感谢进阶!

推荐答案

您可以通过将Json Filter与FilterProvider

You can achieve this by using Json Filter with a FilterProvider

  1. 您使用 批注为您的POJO分配过滤器名称.
  2. 在序列化之前,您附加
  1. you use @JsonFilter annotation to assign a filter name to your POJO.
  2. before serialization, you attach an instance of SimpleBeanPropertyFilter to the filter name. the class has two factory methods for filters that work based on propertry names.

这是注释声明的示例:

@JsonFilter("filter properties by name")
public class So {
    public String alwaysWrite = "alwaysWriteValue";
    public String somtimeIgnore = "somtimeIgnoreValue";
}

这是一种分配过滤器的方法,该过滤器将忽略somtimeIgnore属性

here is a method that assignes a filter that will ignore somtimeIgnore property

public void dontWriteSomtimes(So so) {
    ObjectMapper mapper = new ObjectMapper();
    FilterProvider filters = new SimpleFilterProvider()  
      .addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept("somtimeIgnore"));  
    ObjectWriter writer = mapper.writer(filters);  
    try {
        System.out.println(writer.writeValueAsString(so));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出:
{"alwaysWrite":"alwaysWriteValue"}

这是一种可以编写所有内容的方法:只需分配一个不存在或为空的属性即可:

here is a method that will write everything: just assign a non-existent or empty property:

public void writeEveryting(So so) {
    ObjectMapper mapper = new ObjectMapper();
    FilterProvider filters = new SimpleFilterProvider()  
      .addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept(""));  
    ObjectWriter writer = mapper.writer(filters);  
    try {
        System.out.println(writer.writeValueAsString(so));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出:
{"alwaysWrite":"alwaysWriteValue","somtimeIgnore":"somtimeIgnoreValue"}

这篇关于Json仅在特定方法中忽略属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆