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.