mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Allow pretty printed JSON output (nicer for demos and easier to read)
Defaults to false
This commit is contained in:
parent
3911ce3e29
commit
8a55fbebb2
@ -29,6 +29,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
@ -42,6 +44,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Christian Dupuis
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(HttpMessageConverter.class)
|
||||
@ -60,8 +63,12 @@ public class HttpMessageConvertersAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
@EnableConfigurationProperties(ObjectMappersProperties.class)
|
||||
protected static class ObjectMappers {
|
||||
|
||||
@Autowired
|
||||
private ObjectMappersProperties properties = new ObjectMappersProperties();
|
||||
|
||||
@Autowired
|
||||
private ListableBeanFactory beanFactory;
|
||||
|
||||
@ -90,9 +97,27 @@ public class HttpMessageConvertersAutoConfiguration {
|
||||
ObjectMapper objectMapper) {
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
converter.setObjectMapper(objectMapper);
|
||||
converter.setPrettyPrint(this.properties.isJsonPrettyPrint());
|
||||
return converter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link ConfigurationProperties} to configure {@link HttpMessageConverter}s.
|
||||
*/
|
||||
@ConfigurationProperties(name = "http.mappers", ignoreUnknownFields = false)
|
||||
public static class ObjectMappersProperties {
|
||||
|
||||
private boolean jsonPrettyPrint = false;
|
||||
|
||||
public void setJsonPrettyPrint(boolean jsonPrettyPrint) {
|
||||
this.jsonPrettyPrint = jsonPrettyPrint;
|
||||
}
|
||||
|
||||
public boolean isJsonPrettyPrint() {
|
||||
return this.jsonPrettyPrint;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user