Reputation: 799
The goal is to make Jackson format response from controllers with PrettyPrint. Thats my configuration for it:
@EnableWebMvc
@Configuration
public class JacksonConfig extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
}
}
}
Thats how a controller looks like:
@RequestMapping(value = "/facebook", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Map<String, Object> authorizeViaFacebook(
@RequestParam(value = "token") String token) throws DefaultException{
Facebook facebook = this.facebookUtility.getFacebook(token);
org.springframework.social.facebook.api.User facebookUserProfile = facebook.userOperations().getUserProfile("me");
User loggedInUser = User.signInWithFacebookProfile(facebookUserProfile);
return ImmutableMap.of("token", loggedInUser.tokenForAuthentication(), "user", loggedInUser);
}
But no matter what I do it still prints it straight. I tried different configurations but still no success.
Here is a POM file for Jackson:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
Spring version is 4.2.4.RELEASE.
How do I make Jackson use PrettyPrint format?
Upvotes: 4
Views: 2082
Reputation: 773
spring-boot allows jackson configuration through spring.jackson.*
properties in .properties files.
spring.jackson.serialization.indent-output=true
We can also @Autowire
this configured ObjectMapper
in @Components. If global configuration is not intended then objectMapper.writerWithDefaultPrettyPrinter()
turns on the default indentation.
Tested with spring-boot 1.5.8.RELEASE which uses jackson-databind-2.6.5.jar.
Upvotes: 2
Reputation: 1078
To add to @Jenky answer, here is example how to configure Jackson's mapper for pretty print using code approach. I'm using spring-boot-starter-1.3.3.RELEASE
in this example. Simply have this class on your classpath and Spring Boot will pick it up auto-magically.
@Configuration
public class CustomWebMvcConfiguration extends WebMvcConfigurationSupport {
@Override
protected void extendMessageConverters( List<HttpMessageConverter<?>> converters ) {
for ( HttpMessageConverter<?> converter : converters ) {
if ( converter instanceof MappingJackson2HttpMessageConverter ) {
MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;
jacksonConverter.setPrettyPrint( true );
}
}
}
}
Upvotes: 1
Reputation: 146
I’ve faced a similar problem once when trying to config a Jackson’s object mapper in a Roo generated project. You need to put those snippet into webmvc-config to replace the default objectmapper with your custom one:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="objectMapper">
<array>
<bean class="com.yourproject.example.CustomObjectMapper"/>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Hope it helps. To be able to configure Jackson with a code based config you need setup WebApplicationInitializer You can find more info by link:http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html
Upvotes: 3