From 51cf4e5d7412b605e813a5c0d681ddd164824ce1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 5 Jun 2018 10:30:15 +0200 Subject: [PATCH] Polish "Add support to set visibility via JacksonProperties" Closes gh-13214 --- .../jackson/JacksonAutoConfiguration.java | 12 +++++----- .../jackson/JacksonProperties.java | 24 +++++++++---------- .../JacksonAutoConfigurationTests.java | 8 +++---- .../appendix-application-properties.adoc | 2 +- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index aa4320b06d..9e72b7c07c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -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 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 accessors) { - accessors.forEach(builder::visibility); - } - private static Collection getBeans(ListableBeanFactory beanFactory, Class type) { return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java index 067c3076e9..624f942dc7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java @@ -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 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 accessor = new EnumMap<>( - PropertyAccessor.class); - public String getDateFormat() { return this.dateFormat; } @@ -137,6 +138,10 @@ public class JacksonProperties { this.propertyNamingStrategy = propertyNamingStrategy; } + public Map getVisibility() { + return this.visibility; + } + public Map getSerialization() { return this.serialization; } @@ -182,11 +187,4 @@ public class JacksonProperties { this.locale = locale; } - public Map getAccessor() { - return this.accessor; - } - - public void setAccessor(Map accessor) { - this.accessor = accessor; - } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java index 8d5405993d..4e9e90e478 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java @@ -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; } + } } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 86fb51842e..d01e0a6731 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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.