From f51b304c2a12949f1823609280df0ef8bb8ece00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20J=2E=20Garc=C3=ADa?= Date: Thu, 13 Nov 2014 16:01:56 -0300 Subject: [PATCH 1/2] Update JackonAutoConfiguration to apply all http.mappers properties Previously, only the http.mappers.json-sort-keys property was applied by JacksonAutoConfiguration. This commit updates it to also apply the http.mappers.json-pretty-print property as well. See #1919 --- .../boot/autoconfigure/jackson/JacksonAutoConfiguration.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index 24a436000f..297f0b5bb2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -90,6 +90,9 @@ public class JacksonAutoConfiguration { objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); } + if (this.properties.isJsonPrettyPrint()) { + objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); + } return objectMapper; } From 9eae29938c3b4b469db9f7e1b9cda1405d5069c9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 18 Nov 2014 11:47:29 +0000 Subject: [PATCH 2/2] Test that http.mappers props are applied by JacksonAutoConfiguration Closes gh-1919 --- .../JacksonAutoConfigurationTests.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java index 6cbe098ea3..c5f51d03e9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; +import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -35,6 +36,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.joda.JodaModule; @@ -45,6 +47,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.verify; @@ -101,6 +104,29 @@ public class JacksonAutoConfigurationTests { assertEquals("{\"foo\":\"bar\"}", mapper.writeValueAsString(new Foo())); } + @Test + public void httpMappersJsonPrettyPrintIsApplied() { + this.context.register(JacksonAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "http.mappers.json-pretty-print:true"); + this.context.refresh(); + ObjectMapper objectMapper = this.context.getBean(ObjectMapper.class); + assertTrue(objectMapper.getSerializationConfig().isEnabled( + SerializationFeature.INDENT_OUTPUT)); + } + + @Test + public void httpMappersJsonSortKeysIsApplied() { + this.context.register(JacksonAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "http.mappers.json-sort-keys:true"); + this.context.refresh(); + ObjectMapper objectMapper = this.context.getBean(ObjectMapper.class); + assertTrue(objectMapper.getSerializationConfig().isEnabled( + SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)); + + } + @Configuration protected static class ModulesConfig {