【问题标题】:Excluding null fields in pojo response在 pojo 响应中排除空字段
【发布时间】:2015-11-20 06:29:59
【问题描述】:

我想从 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;
	}

	
	}

Transaction History BO 类型的数组列表用于 Transaction History Response pojo。这是我在响应中显示的确切 pojo。我想排除 Transaction History BO 中具有空值的字段。强> 我试过@JsonInclude(JsonInclude.Include.NON_NULL)。它不工作.. 也尝试使用 JsonSerialize,但已弃用。使用的杰克逊版本是 2.2.2。

任何帮助将不胜感激..请帮助..

【问题讨论】:

    标签: java json jackson pojo


    【解决方案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>
    

    无论有没有这个都经过测试,它都可以工作。

    【讨论】:

    • ,感谢您的帮助。在我的原始帖子中,我声明 json 在 TransactionHistoryBO 中的类级别本身包含注释。它不起作用。无论如何我可以在哪里使用您所写的对象映射器概念?仅供参考:我正在使用 rest easy。它会自动将 pojo 转换为 json。
    • 您是否像我的示例中一样将 jsoninclude 注释添加到 TransactionHistoryResponse 类?
    • 已更新我的答案,请再次查看。
    • 我会试试这个并告诉你..:)
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 2021-08-22
    • 2015-01-16
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多