****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;
}
}
@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);
}
}
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.package.CustomJsonProvider</param-value>
</context-param>
我正在开发运动衫2 RESTful服务 球衣2 v2。22.2 Spring3 v3。2.16 Jax rs v2。0.1 如果我收到@QueryParam template=“summary”,我需要排除一些GameDTO响应字段。 例如: 获取/Rest/v1/游戏?city=1必须返回: 然而 获取/Rest/v1/游戏?城市=1?模板=必须返回摘要: 如何排除字段?
如果我们有如下查询 返回预期的结果(如下所示) 正如您所看到的,查询是针对和我们希望从结果中排除其他字段,但只包括和的字段 并排除其他字段,例如,以下是一些不是或(传递到查询中的字段)的字段-从
我有一个类,其中一些字段可能是空的。我想忽略csv中的这些字段。寻找与Jackson中的@jsoninclude(value=include.non_null)类似的功能。CSV中也不应出现空字段列标题
问题内容: 在.NET Core 1.0(所有API响应)的全局级别上,如何配置Startup.cs,以便在JSON响应中删除/忽略空字段? 使用Newtonsoft.Json,您可以将以下属性应用于属性,但我希望避免将其添加到每个属性中: 问题答案: [.NET Core 1.0] 在Startup.cs中,可以将JsonOptions附加到服务集合,并在其中设置各种配置,包括删除空值: [.N
如何从Jackson在中生成的json响应中排除所有空json对象、数组或空对象的数组? 我正在为一个客户端构建一个Spring Boot REST API。API向数据库发送请求,并且必须生成JSON响应。 DAO层发送本机SQL查询,并以列表形式接收巨大的DB结果。开发人员必须使用索引手动将此结果映射到Java对象(参见下面的代码) SQL查询返回大量空值(这些查询不能修改)。由于这些空值,只