在JAX-RS和Jackson中优化排除空值、空列表、数组的方法有以下几种:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@XmlRootElement(name = "person", namespace = "http://example.com")
public class Person {
private String name;
private int age;
private List<String> hobbies;
// getters and setters
}
@JsonRootName(value = "person")
public class Person {
private String name;
private int age;
private List<String> hobbies;
// getters and setters
}
ObjectWriter writer = new ObjectMapper().writer();
writer.setSerializationInclusion(JsonInclude.Include.NON_NULL);
writer.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
JAXRSSpecification.getInstance().setSerializationInclusion(JsonInclude.Include.NON_NULL);
JAXRSSpecification.getInstance().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
以上是排除空值、空列表、数组的一些常见方法,可以根据具体情况进行选择。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云