Polish "Add support to set visibility via JacksonProperties"

Closes gh-13214
pull/13355/head
Stephane Nicoll 7 years ago
parent ca3659059a
commit 51cf4e5d74

@ -243,6 +243,7 @@ public class JacksonAutoConfiguration {
builder.timeZone(this.jacksonProperties.getTimeZone());
}
configureFeatures(builder, FEATURE_DEFAULTS);
configureVisibility(builder, this.jacksonProperties.getVisibility());
configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper());
@ -252,7 +253,6 @@ public class JacksonAutoConfiguration {
configurePropertyNamingStrategy(builder);
configureModules(builder);
configureLocale(builder);
configureVisibility(builder, this.jacksonProperties.getAccessor());
}
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
@ -269,6 +269,11 @@ public class JacksonAutoConfiguration {
});
}
private void configureVisibility(Jackson2ObjectMapperBuilder builder,
Map<PropertyAccessor, JsonAutoDetect.Visibility> accessors) {
accessors.forEach(builder::visibility);
}
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {
// We support a fully qualified class name extending DateFormat or a date
// pattern string value
@ -351,11 +356,6 @@ public class JacksonAutoConfiguration {
}
}
private void configureVisibility(Jackson2ObjectMapperBuilder builder,
Map<PropertyAccessor, JsonAutoDetect.Visibility> accessors) {
accessors.forEach(builder::visibility);
}
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
Class<T> type) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)

@ -61,6 +61,13 @@ public class JacksonProperties {
*/
private String propertyNamingStrategy;
/**
* Jackson visibility thresholds that can be used to limit which methods (and fields)
* are auto-detected.
*/
private Map<PropertyAccessor, JsonAutoDetect.Visibility> visibility = new EnumMap<>(
PropertyAccessor.class);
/**
* Jackson on/off features that affect the way Java objects are serialized.
*/
@ -107,12 +114,6 @@ public class JacksonProperties {
*/
private Locale locale;
/**
* Jackson visibilities to auto-detect properties.
*/
private Map<PropertyAccessor, JsonAutoDetect.Visibility> accessor = new EnumMap<>(
PropertyAccessor.class);
public String getDateFormat() {
return this.dateFormat;
}
@ -137,6 +138,10 @@ public class JacksonProperties {
this.propertyNamingStrategy = propertyNamingStrategy;
}
public Map<PropertyAccessor, JsonAutoDetect.Visibility> getVisibility() {
return this.visibility;
}
public Map<SerializationFeature, Boolean> getSerialization() {
return this.serialization;
}
@ -182,11 +187,4 @@ public class JacksonProperties {
this.locale = locale;
}
public Map<PropertyAccessor, JsonAutoDetect.Visibility> getAccessor() {
return this.accessor;
}
public void setAccessor(Map<PropertyAccessor, JsonAutoDetect.Visibility> accessor) {
this.accessor = accessor;
}
}

@ -461,15 +461,14 @@ public class JacksonAutoConfigurationTests {
@Test
public void writeWithVisibility() {
this.contextRunner.withPropertyValues("spring.jackson.accessor.getter:NONE",
"spring.jackson.accessor.field:ANY")
.run((context) -> {
this.contextRunner.withPropertyValues("spring.jackson.visibility.getter:none",
"spring.jackson.visibility.field:any").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
String json = mapper.writeValueAsString(new VisibilityBean());
assertThat(json).contains("property1");
assertThat(json).contains("property2");
assertThat(json).doesNotContain("property3");
});
});
}
private void assertParameterNamesModuleCreatorBinding(Mode expectedMode,
@ -636,6 +635,7 @@ public class JacksonAutoConfigurationTests {
public String getProperty3() {
return null;
}
}
}

@ -329,7 +329,6 @@ content into your application. Rather, pick only the properties that you need.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
# JACKSON ({sc-spring-boot-autoconfigure}/jackson/JacksonProperties.{sc-ext}[JacksonProperties])
spring.jackson.accessor.*= # Jackson visibilities to auto-detect properties.
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
spring.jackson.default-property-inclusion= # Controls the inclusion of properties during serialization. Configured with one of the values in Jackson's JsonInclude.Include enumeration.
spring.jackson.deserialization.*= # Jackson on/off features that affect the way Java objects are deserialized.
@ -341,6 +340,7 @@ content into your application. Rather, pick only the properties that you need.
spring.jackson.property-naming-strategy= # One of the constants on Jackson's PropertyNamingStrategy. Can also be a fully-qualified class name of a PropertyNamingStrategy subclass.
spring.jackson.serialization.*= # Jackson on/off features that affect the way Java objects are serialized.
spring.jackson.time-zone= # Time zone used when formatting dates. For instance, "America/Los_Angeles" or "GMT+10".
spring.jackson.visibility.*= # Jackson visibility thresholds that can be used to limit which methods (and fields) are auto-detected.
# GSON ({sc-spring-boot-autoconfigure}/gson/GsonProperties.{sc-ext}[GsonProperties])
spring.gson.date-format= # Format to use when serializing Date objects.

Loading…
Cancel
Save