cri*_*sli 19 null serialization spring jackson spring-boot
在Spring Boot 1.2.3中,我们可以通过属性文件自定义Jackson ObjectMapper.但是我没有找到一个属性可以设置Jackson在将Object序列化为JSON字符串时忽略null值.
spring.jackson.deserialization.*= # see Jackson's DeserializationFeature
spring.jackson.generator.*= # see Jackson's JsonGenerator.Feature
spring.jackson.mapper.*= # see Jackson's MapperFeature
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
spring.jackson.serialization.*=
Run Code Online (Sandbox Code Playgroud)
我想存档相同的代码,如
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
Run Code Online (Sandbox Code Playgroud)
cju*_*gel 51
将以下行添加到您的application.properties文件中.
spring.jackson.default属性模型 - 包裹体= non_null
对于2.7之前的Jackson版本:
spring.jackson.serialization系夹杂物= non_null
小智 15
在弃用之前,这是一个很好的解决方案:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
但现在你应该使用:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ClassName {
...
你可以看看这里:https: //fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html
小智 10
对于Spring Boot 1.4.x,您可以在application.properties中包含以下行
spring.jackson.default-property-inclusion=non_null
这是对Spring Boot 1.3.0的增强。
因此,不幸的是,您需要在1.2.3上以编程方式对其进行配置
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Shop {
//...
}
Run Code Online (Sandbox Code Playgroud)
类范围的,
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyModel { .... }
Run Code Online (Sandbox Code Playgroud)
物业范围:
public class MyModel {
.....
@JsonInclude(JsonInclude.Include.NON_NULL)
private String myProperty;
.....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31112 次 |
| 最近记录: |