Merge pull request #498 from veryangryant/json-sort-keys

* json-sort-keys:
  Add support for sorting json keys
pull/521/merge
Phillip Webb 11 years ago
commit cb52cb5555

@ -21,11 +21,16 @@ import org.springframework.http.converter.HttpMessageConverter;
/** /**
* Configuration properties to configure {@link HttpMessageConverter}s. * Configuration properties to configure {@link HttpMessageConverter}s.
*
* @author Dave Syer
* @author Piotr Maj
*/ */
@ConfigurationProperties(name = "http.mappers", ignoreUnknownFields = false) @ConfigurationProperties(name = "http.mappers", ignoreUnknownFields = false)
public class HttpMapperProperties { public class HttpMapperProperties {
private boolean jsonPrettyPrint = false; private boolean jsonPrettyPrint;
private boolean jsonSortKeys;
public void setJsonPrettyPrint(boolean jsonPrettyPrint) { public void setJsonPrettyPrint(boolean jsonPrettyPrint) {
this.jsonPrettyPrint = jsonPrettyPrint; this.jsonPrettyPrint = jsonPrettyPrint;
@ -35,4 +40,12 @@ public class HttpMapperProperties {
return this.jsonPrettyPrint; return this.jsonPrettyPrint;
} }
public void setJsonSortKeys(boolean jsonSortKeys) {
this.jsonSortKeys = jsonSortKeys;
}
public boolean isJsonSortKeys() {
return this.jsonSortKeys;
}
} }

@ -38,12 +38,14 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s. * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s.
* *
* @author Dave Syer * @author Dave Syer
* @author Christian Dupuis * @author Christian Dupuis
* @author Piotr Maj
*/ */
@Configuration @Configuration
@ConditionalOnClass(HttpMessageConverter.class) @ConditionalOnClass(HttpMessageConverter.class)
@ -87,7 +89,12 @@ public class HttpMessageConvertersAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Primary @Primary
public ObjectMapper jacksonObjectMapper() { public ObjectMapper jacksonObjectMapper() {
return new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
if (this.properties.isJsonSortKeys()) {
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS,
true);
}
return objectMapper;
} }
@Bean @Bean

@ -64,6 +64,7 @@ server.tomcat.max-threads = 0 # number of threads in protocol handler
# SPRING MVC (HttpMapperProperties) # SPRING MVC (HttpMapperProperties)
http.mappers.json-pretty-print=false # pretty print JSON http.mappers.json-pretty-print=false # pretty print JSON
http.mappers.json-sort-keys=falose # sort keys
spring.view.prefix= # MVC view prefix spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix spring.view.suffix= # ... and suffix
spring.resources.cache-period= # cache timeouts in headers sent to browser spring.resources.cache-period= # cache timeouts in headers sent to browser

Loading…
Cancel
Save