排除在POJO响应空字段
问题描述:
我想从一个POJO排除在POJO响应空字段
**** TransactionHistoryBO POJO的**排除空字段
package main.java.com.as.model;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TransactionHistoryBO
{
\t private String processId;
\t private String dateTime;
\t private Integer status;
\t private Double pointsEarned;
\t private String productName;
\t private String receiptNumber;
\t
\t public String getProcessId() {
\t \t return processId;
\t }
\t public void setProcessId(String processId) {
\t \t this.processId = processId;
\t }
\t public String getDateTime() {
\t \t return dateTime;
\t }
\t public void setDateTime(String dateTime) {
\t \t this.dateTime = dateTime;
\t }
\t public Integer getStatus() {
\t \t return status;
\t }
\t public void setStatus(Integer status) {
\t \t this.status = status;
\t }
\t
\t public Double getPointsEarned() {
\t \t return pointsEarned;
\t }
\t public void setPointsEarned(Double pointsEarned) {
\t \t this.pointsEarned = pointsEarned;
\t }
\t public String getProductName() {
\t \t return productName;
\t }
\t
\t public void setProductName(String productName) {
\t \t this.productName = productName;
\t }
\t
\t public String getReceiptNumber() {
\t \t return receiptNumber;
\t }
\t public void setReceiptNumber(String receiptNumber) {
\t \t this.receiptNumber = receiptNumber;
\t }
}
**
交易历史回复pojo
类型交易记录BO的10public class TransactionHistoryResponse
{
\t private ArrayList<TransactionHistoryBO> transactions;
\t
\t @JsonInclude(JsonInclude.Include.NON_NULL)
\t public ArrayList<TransactionHistoryBO> getTransactions() {
\t \t return transactions;
\t }
\t @JsonInclude(Include.NON_NULL)
\t public void setTransactions(ArrayList<TransactionHistoryBO> transactions) {
\t \t this.transactions = transactions;
\t }
\t
\t }
数组列表在交易历史响应pojo.This用的是我在response.I我出确切的POJO想排除与空值领域在交易历史BO中。 我试着用@JsonInclude(JsonInclude.Include.NON_NULL)。它不工作.. 也试过用JsonSerialize,但它被弃用.Jackson使用的版本是2.2.2。
任何帮助将appreciated..please帮助..
答
@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注释添加到类而不是属性。
UPDATE:
添加自定义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。它会自动将pojo转换为json。 – Jill
您是否像在我的示例中那样将jsoninclude注释添加到TransactionHistoryResponse类中? – Ghokun
是的..它不工作.. – Jill