Jackson PrettyPrint for Spring 4

The goal is to format the Jackson format response from the controllers using PrettyPrint. This is my configuration:

@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);
            }
        }
    }
} 

Here's what the 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, he still prints it directly. I tried different configurations, but still have not succeeded.

Here is the 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 4.2.4.RELEASE.

How to get Jackson to use PrettyPrint format?

+4
source share
2 answers

Ive , Jacksons , Roo. webmvc-config, objectmapper :

<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>

, . Jackson , WebApplicationInitializer : http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html

+3

@Jenky, , Jackson . spring-boot-starter-1.3.3.RELEASE . , Spring Boot .

@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 );

            }

        }

    }

}
+1

Source: https://habr.com/ru/post/1625313/


All Articles