Reputation: 3063
Using Spring Boot 1.1.6.RELEASE I cannot get JSON to pretty print from my MVC controllers - something that should have taken less than a minute (and that we've configured countless times in previous Spring projects) has taken a number of hours.
I've tried various things including:
1) Using the documented auto-configuration in application.properties
http.mappers.jsonPrettyPrint=true
Has no effect
2) Creating my own Jackson instance
@Bean
MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter mc = ...
mc.setPrettyPrint(**true**);
return mc;
}
Has no effect
3) Injecting the containers ObjectMapper and configuring it
@Inject ObjectMapper objectMapper;
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
and
objectMapper.withDefaultPrettyPrinter();
Both have no effect
4) Turning off Spring Actuator (in case it was overwriting configuration)
Has no effect
5) Checked, double checked, triple checked I'm calling the right host, shut down to confirm connection refused, changed output to confirm code is the code I'm running
Still no way to configure JSON Pretty printing - has anyone seen this, could it be related to a side effect in Spring IO (1.0.2.RELEASE) or Jackson (fasterxml jackson-core 2.3.4)?
Upvotes: 1
Views: 1934
Reputation: 556
Did you try it like this:
@Configuration
public class TimesheetMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setWriteAcceptCharset(false);
converters.add(stringConverter);
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new ResourceHttpMessageConverter());
converters.add(new SourceHttpMessageConverter<Source>());
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(jackson2Converter());
}
@Bean
public MappingJackson2HttpMessageConverter jackson2Converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
return objectMapper;
}
}
Upvotes: 2