排除 pojo 响应中的空字段
Excluding null fields in pojo response
我想从 pojo 中排除空字段
****交易历史BO 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;
}
}
事务历史记录 BO 类型的数组列表用于事务历史响应 pojo.This 是我在 response.I 中显示的确切 pojo 想排除 字段为 null交易历史 BO 中的值。
我尝试使用@JsonInclude(JsonInclude.Include.NON_NULL)。它不工作..
也尝试使用 JsonSerialize,但它是 deprecated.Jackson 使用的版本是 2.2.2.
任何帮助将不胜感激..请帮助..
@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>
在有和没有这个的情况下都进行了测试,它都有效。
我想从 pojo 中排除空字段
****交易历史BO 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;
}
}
事务历史记录 BO 类型的数组列表用于事务历史响应 pojo.This 是我在 response.I 中显示的确切 pojo 想排除 字段为 null交易历史 BO 中的值。 我尝试使用@JsonInclude(JsonInclude.Include.NON_NULL)。它不工作.. 也尝试使用 JsonSerialize,但它是 deprecated.Jackson 使用的版本是 2.2.2.
任何帮助将不胜感激..请帮助..
@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>
在有和没有这个的情况下都进行了测试,它都有效。