From ca3659059aa8c648603d2f6a5318860327aa55e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 16 May 2018 01:10:22 -0500 Subject: [PATCH] Add support to set visibility via JacksonProperties This commit allows to configure properties visibility. See gh-13214 --- .../jackson/JacksonAutoConfiguration.java | 9 +++++++ .../jackson/JacksonProperties.java | 15 ++++++++++++ .../JacksonAutoConfigurationTests.java | 24 +++++++++++++++++++ .../appendix-application-properties.adoc | 1 + 4 files changed, 49 insertions(+) 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 5a52655ba6..aa4320b06d 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 @@ -27,7 +27,9 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; @@ -73,6 +75,7 @@ import org.springframework.util.ReflectionUtils; * @author Sebastien Deleuze * @author Johannes Edmeier * @author Phillip Webb + * @author EddĂș MelĂ©ndez * @since 1.1.0 */ @Configuration @@ -249,6 +252,7 @@ public class JacksonAutoConfiguration { configurePropertyNamingStrategy(builder); configureModules(builder); configureLocale(builder); + configureVisibility(builder, this.jacksonProperties.getAccessor()); } private void configureFeatures(Jackson2ObjectMapperBuilder builder, @@ -347,6 +351,11 @@ 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 be580863de..067c3076e9 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 @@ -21,7 +21,9 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -105,6 +107,12 @@ 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; } @@ -174,4 +182,11 @@ 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 9e7e9754a0..8d5405993d 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 @@ -459,6 +459,19 @@ public class JacksonAutoConfigurationTests { }); } + @Test + public void writeWithVisibility() { + this.contextRunner.withPropertyValues("spring.jackson.accessor.getter:NONE", + "spring.jackson.accessor.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, Class... configClasses) { this.contextRunner.withUserConfiguration(configClasses).run((context) -> { @@ -614,4 +627,15 @@ public class JacksonAutoConfigurationTests { } + private static class VisibilityBean { + + private String property1; + + public String property2; + + 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 1c605b4d4f..86fb51842e 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,6 +329,7 @@ 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.