Merge branch 'jackson-mapper'
This commit is contained in:
commit
1c4f1f0ecb
|
|
@ -109,7 +109,8 @@ public class JacksonAutoConfiguration {
|
|||
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
||||
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
|
||||
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
||||
if (this.httpMapperProperties.isJsonSortKeys()) {
|
||||
Boolean isJsonSortKeys = this.httpMapperProperties.isJsonSortKeys();
|
||||
if (isJsonSortKeys != null && isJsonSortKeys) {
|
||||
builder.featuresToEnable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
|
||||
}
|
||||
configureFeatures(builder, this.jacksonProperties.getDeserialization());
|
||||
|
|
|
|||
|
|
@ -24,27 +24,28 @@ import org.springframework.http.converter.HttpMessageConverter;
|
|||
*
|
||||
* @author Dave Syer
|
||||
* @author Piotr Maj
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "http.mappers", ignoreUnknownFields = false)
|
||||
public class HttpMapperProperties {
|
||||
|
||||
private boolean jsonPrettyPrint;
|
||||
private Boolean jsonPrettyPrint;
|
||||
|
||||
private boolean jsonSortKeys;
|
||||
private Boolean jsonSortKeys;
|
||||
|
||||
public void setJsonPrettyPrint(boolean jsonPrettyPrint) {
|
||||
public void setJsonPrettyPrint(Boolean jsonPrettyPrint) {
|
||||
this.jsonPrettyPrint = jsonPrettyPrint;
|
||||
}
|
||||
|
||||
public boolean isJsonPrettyPrint() {
|
||||
public Boolean isJsonPrettyPrint() {
|
||||
return this.jsonPrettyPrint;
|
||||
}
|
||||
|
||||
public void setJsonSortKeys(boolean jsonSortKeys) {
|
||||
public void setJsonSortKeys(Boolean jsonSortKeys) {
|
||||
this.jsonSortKeys = jsonSortKeys;
|
||||
}
|
||||
|
||||
public boolean isJsonSortKeys() {
|
||||
public Boolean isJsonSortKeys() {
|
||||
return this.jsonSortKeys;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -78,7 +78,9 @@ public class HttpMessageConvertersAutoConfiguration {
|
|||
ObjectMapper objectMapper) {
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
converter.setObjectMapper(objectMapper);
|
||||
converter.setPrettyPrint(this.properties.isJsonPrettyPrint());
|
||||
if (this.properties.isJsonPrettyPrint() != null) {
|
||||
converter.setPrettyPrint(this.properties.isJsonPrettyPrint());
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
|
@ -99,7 +101,9 @@ public class HttpMessageConvertersAutoConfiguration {
|
|||
Jackson2ObjectMapperBuilder builder) {
|
||||
MappingJackson2XmlHttpMessageConverter converter = new MappingJackson2XmlHttpMessageConverter();
|
||||
converter.setObjectMapper(builder.createXmlMapper(true).build());
|
||||
converter.setPrettyPrint(this.properties.isJsonPrettyPrint());
|
||||
if (this.properties.isJsonPrettyPrint() != null) {
|
||||
converter.setPrettyPrint(this.properties.isJsonPrettyPrint());
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ import java.util.List;
|
|||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
@ -34,6 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.google.gson.Gson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
|
|
@ -155,6 +158,30 @@ public class HttpMessageConvertersAutoConfigurationTests {
|
|||
assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpMapperPropertiesAreNotAppliedWhenNotConfigured() throws Exception {
|
||||
this.context.register(JacksonObjectMapperConfig.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
MappingJackson2HttpMessageConverter converter = this.context
|
||||
.getBean(MappingJackson2HttpMessageConverter.class);
|
||||
assertNull(new DirectFieldAccessor(converter)
|
||||
.getPropertyValue("prettyPrint"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpMapperPropertiesAreAppliedWhenConfigured() throws Exception {
|
||||
this.context.register(JacksonObjectMapperConfig.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"http.mappers.jsonPrettyPrint:true");
|
||||
this.context.refresh();
|
||||
MappingJackson2HttpMessageConverter converter = this.context
|
||||
.getBean(MappingJackson2HttpMessageConverter.class);
|
||||
assertTrue((Boolean) new DirectFieldAccessor(converter)
|
||||
.getPropertyValue("prettyPrint"));
|
||||
}
|
||||
|
||||
private void assertConverterBeanExists(Class<?> type, String beanName) {
|
||||
assertEquals(1, this.context.getBeansOfType(type).size());
|
||||
List<String> beanNames = Arrays.asList(this.context.getBeanDefinitionNames());
|
||||
|
|
|
|||
Loading…
Reference in New Issue