排除pojo响应中的空字段

时间:2015-11-20 06:29:59

标签: java json jackson pojo

我想从pojo

中排除空字段

**** TransactionHistoryBO Pojo **



package main.java.com.as.model;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class TransactionHistoryBO 
{
	 private String processId;
	 private String dateTime;
	 private Integer status;
	 private Double pointsEarned;
	 private String productName;
	 private String receiptNumber;
	  
	public String getProcessId() {
		return processId;
	}
	public void setProcessId(String processId) {
		this.processId = processId;
	}


	public String getDateTime() {
		return dateTime;
	}
	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public Double getPointsEarned() {
		return pointsEarned;
	}

	public void setPointsEarned(Double pointsEarned) {
		this.pointsEarned = pointsEarned;
	}

	public String getProductName() {
		return productName;
	}
	
	public void setProductName(String productName) {
		this.productName = productName;
	}
	
	public String getReceiptNumber() {
		return receiptNumber;
	}
	public void setReceiptNumber(String receiptNumber) {
		this.receiptNumber = receiptNumber;
	}



}




**

交易历史回复pojo



public class TransactionHistoryResponse 
{
	private ArrayList<TransactionHistoryBO> transactions;
	
	@JsonInclude(JsonInclude.Include.NON_NULL)
	public ArrayList<TransactionHistoryBO> getTransactions() {
		return transactions;
	}
	@JsonInclude(Include.NON_NULL)
	public void setTransactions(ArrayList<TransactionHistoryBO> transactions) {
		this.transactions = transactions;
	}

	
	}
&#13;
&#13;
&#13;

交易历史BO类型的数组列表在交易历史响应pojo中使用。这是我在响应中显示的确切pojo。我想在交易历史BO中排除具有空值的字段。 我尝试使用@JsonInclude(JsonInclude.Include.NON_NULL)。它无法正常工作.. 也尝试使用JsonSerialize,但它已被弃用。使用的Jackson版本是2.2.2。

任何帮助都会受到赞赏..请帮助..

1 个答案:

答案 0 :(得分:1)

@JsonInclude(JsonInclude.Include.NON_NULL)
public class TransactionHistoryBO { ... }

@JsonInclude(JsonInclude.Include.NON_NULL)
public class TransactionHistoryResponse { ... }

public class App {

    public static void main(String... args) throws JsonProcessingException {

        ObjectMapper om = new ObjectMapper();

        TransactionHistoryResponse thr = new TransactionHistoryResponse();
        TransactionHistoryBO thbo = new TransactionHistoryBO();
        thbo.setProductName("TEST");
        thr.setTransactions(new ArrayList<TransactionHistoryBO>());
        thr.getTransactions().add(thbo);
        System.out.print(om.writerWithDefaultPrettyPrinter().writeValueAsString(thr));
    }

}

产生输出:

{
  "transactions" : [ {
    "productName" : "TEST"
  } ]
}

没有使用其他注释。只需将@JsonInclude注释添加到类而不是属性。

<强>更新

将自定义JacksonJsonProvider添加到您的应用程序

@Provider
public class CustomJsonProvider extends ResteasyJackson2Provider {

    @Override
    public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {

        ObjectMapper mapper = locateMapper(type, mediaType);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        super.writeTo(value, type, genericType, annotations, mediaType, httpHeaders, entityStream);
    }

}

在web.xml中注册此提供程序

<context-param> 
    <param-name>resteasy.providers</param-name> 
    <param-value>com.package.CustomJsonProvider</param-value> 
</context-param>

使用和不使用它进行测试,它可以正常工作。