Springboot统一配置Jackson

经常要为接口响应对象设置属性,序列化的时候是不是包含空值,反序列化的时候是否忽略不认识的字段。所以,必须要手动制定ObjectMapper或者在类上声明

@JsonInclude(Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true)

虽然不算复杂,但既然统一了规则,那就来个统一设定吧。

在springboo1.5+以上的版本可以这么设置Jackson的一些属性配置

spring: jackson: serialization: WRITE_DATES_AS_TIMESTAMPS: false default-property-inclusion: non_null

更多配置参见 org.springframework.boot.autoconfigure.jackson.JacksonProperties

@ConfigurationProperties( prefix = "spring.jackson" ) public class JacksonProperties { private String dateFormat; private String jodaDateTimeFormat; private String propertyNamingStrategy; private Map<SerializationFeature, Boolean> serialization = new HashMap(); private Map<DeserializationFeature, Boolean> deserialization = new HashMap(); private Map<MapperFeature, Boolean> mapper = new HashMap(); private Map<Feature, Boolean> parser = new HashMap(); private Map<com.fasterxml.jackson.core.JsonGenerator.Feature, Boolean> generator = new HashMap(); private Include defaultPropertyInclusion; private TimeZone timeZone = null; private Locale locale; //省略 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzzdy.html