问题
I want to exclude null fields from a 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;
}
}
**
Transaction History Response 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;
}
}
Array list of type Transaction History BO is used in Transaction History Response pojo.This is the exact pojo that i am showing in response.I would like to exclude the fields with null values in Transaction History BO. I tried with @JsonInclude(JsonInclude.Include.NON_NULL).It is not working.. Also tried with JsonSerialize,but it is deprecated.Jackson version used is 2.2.2.
Any help would be appreciated..please help..
回答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));
}
}
Produces output :
{
"transactions" : [ {
"productName" : "TEST"
} ]
}
No other annotation is used. Just add @JsonInclude annotation to classes not properties.
UPDATE:
Add a custom JacksonJsonProvider to your application
@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);
}
}
Register this provider in your web.xml
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.package.CustomJsonProvider</param-value>
</context-param>
Tested with and without this and it works.
来源:https://stackoverflow.com/questions/33820327/excluding-null-fields-in-pojo-response