Commit 1dcf18b1 authored by Andy Wilkinson's avatar Andy Wilkinson

Allow Jackson's serialization inclusion to be configured via the env

This commit adds support for configuring an ObjectMapper's
serialization inclusion using the environment via the
spring.jackson.serialization-inclusion property. The property's value
should be one of the values on the JsonInclude.Include enumeration.
Relaxed binding of the property value to the enum is supported. For
example:

spring.jackson.serialization-inclusion: non_null

Closes gh-2532
parent 60734548
...@@ -176,6 +176,10 @@ public class JacksonAutoConfiguration { ...@@ -176,6 +176,10 @@ public class JacksonAutoConfiguration {
if (isJsonPrettyPrint != null && isJsonPrettyPrint) { if (isJsonPrettyPrint != null && isJsonPrettyPrint) {
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT); builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
} }
if (this.jacksonProperties.getSerializationInclusion() != null) {
builder.serializationInclusion(this.jacksonProperties
.getSerializationInclusion());
}
configureFeatures(builder, this.jacksonProperties.getDeserialization()); configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization()); configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper()); configureFeatures(builder, this.jacksonProperties.getMapper());
......
...@@ -21,6 +21,7 @@ import java.util.Map; ...@@ -21,6 +21,7 @@ import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
...@@ -45,8 +46,7 @@ public class JacksonProperties { ...@@ -45,8 +46,7 @@ public class JacksonProperties {
/** /**
* Joda date time format string (yyyy-MM-dd HH:mm:ss). If not configured, * Joda date time format string (yyyy-MM-dd HH:mm:ss). If not configured,
* "date-format" will be used as a fallback if it is configured with a format * "date-format" will be used as a fallback if it is configured with a format string.
* string.
*/ */
private String jodaDateTimeFormat; private String jodaDateTimeFormat;
...@@ -82,6 +82,12 @@ public class JacksonProperties { ...@@ -82,6 +82,12 @@ public class JacksonProperties {
*/ */
private Map<JsonGenerator.Feature, Boolean> generator = new HashMap<JsonGenerator.Feature, Boolean>(); private Map<JsonGenerator.Feature, Boolean> generator = new HashMap<JsonGenerator.Feature, Boolean>();
/**
* Controls the inclusion of properties during serialization. Configured with one of
* the values in Jackson's JsonInclude.Include enumeration.
*/
private JsonInclude.Include serializationInclusion;
public String getDateFormat() { public String getDateFormat() {
return this.dateFormat; return this.dateFormat;
} }
...@@ -126,4 +132,12 @@ public class JacksonProperties { ...@@ -126,4 +132,12 @@ public class JacksonProperties {
return this.generator; return this.generator;
} }
public JsonInclude.Include getSerializationInclusion() {
return this.serializationInclusion;
}
public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
this.serializationInclusion = serializationInclusion;
}
} }
...@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
...@@ -387,6 +388,28 @@ public class JacksonAutoConfigurationTests { ...@@ -387,6 +388,28 @@ public class JacksonAutoConfigurationTests {
assertThat(objectMapper.canSerialize(LocalDateTime.class), is(true)); assertThat(objectMapper.canSerialize(LocalDateTime.class), is(true));
} }
@Test
public void defaultSerializationInclusion() {
this.context.register(JacksonAutoConfiguration.class);
this.context.refresh();
ObjectMapper objectMapper = this.context.getBean(
Jackson2ObjectMapperBuilder.class).build();
assertThat(objectMapper.getSerializationConfig().getSerializationInclusion(),
is(JsonInclude.Include.ALWAYS));
}
@Test
public void customSerializationInclusion() {
this.context.register(JacksonAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization-inclusion:non_null");
this.context.refresh();
ObjectMapper objectMapper = this.context.getBean(
Jackson2ObjectMapperBuilder.class).build();
assertThat(objectMapper.getSerializationConfig().getSerializationInclusion(),
is(JsonInclude.Include.NON_NULL));
}
@Configuration @Configuration
protected static class MockObjectMapperConfig { protected static class MockObjectMapperConfig {
......
...@@ -142,6 +142,7 @@ content into your application; rather pick only the properties that you need. ...@@ -142,6 +142,7 @@ content into your application; rather pick only the properties that you need.
spring.jackson.mapper.*= # see Jackson's MapperFeature spring.jackson.mapper.*= # see Jackson's MapperFeature
spring.jackson.parser.*= # see Jackson's JsonParser.Feature spring.jackson.parser.*= # see Jackson's JsonParser.Feature
spring.jackson.serialization.*= # see Jackson's SerializationFeature spring.jackson.serialization.*= # see Jackson's SerializationFeature
spring.jackson.serialization-inclusion= # Controls the inclusion of properties during serialization (see Jackson's JsonInclude.Include)
# THYMELEAF ({sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[ThymeleafAutoConfiguration]) # THYMELEAF ({sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[ThymeleafAutoConfiguration])
spring.thymeleaf.check-template-location=true spring.thymeleaf.check-template-location=true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment