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
pull/2268/merge
Andy Wilkinson 10 years ago
parent 60734548fe
commit 1dcf18b16e

@ -176,6 +176,10 @@ public class JacksonAutoConfiguration {
if (isJsonPrettyPrint != null && isJsonPrettyPrint) {
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
}
if (this.jacksonProperties.getSerializationInclusion() != null) {
builder.serializationInclusion(this.jacksonProperties
.getSerializationInclusion());
}
configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper());

@ -21,6 +21,7 @@ import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
@ -45,8 +46,7 @@ public class JacksonProperties {
/**
* 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
* string.
* "date-format" will be used as a fallback if it is configured with a format string.
*/
private String jodaDateTimeFormat;
@ -82,6 +82,12 @@ public class JacksonProperties {
*/
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() {
return this.dateFormat;
}
@ -126,4 +132,12 @@ public class JacksonProperties {
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;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -387,6 +388,28 @@ public class JacksonAutoConfigurationTests {
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
protected static class MockObjectMapperConfig {

@ -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.parser.*= # see Jackson's JsonParser.Feature
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])
spring.thymeleaf.check-template-location=true

Loading…
Cancel
Save