春季4杰克逊PrettyPrint [英] Jackson PrettyPrint for Spring 4

查看:93
本文介绍了春季4杰克逊PrettyPrint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是通过PrettyPrint使控制器的Jackson格式响应。
这就是我的配置:

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.

这是杰克逊的POM文件:

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>

春季版本为4.2.4.RELEASE。

Spring version is 4.2.4.RELEASE.

如何让Jackson使用PrettyPrint格式?

How do I make Jackson use PrettyPrint format?

推荐答案

在尝试在Roo生成的项目中配置Jackson的对象映射器时,我遇到过类似的问题。您需要将这些代码段放入webmvc-config以使用您的自定义代码替换默认对象映射器:

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>

希望有所帮助。为了能够使用基于代码的配置配置Jackson,您需要设置WebApplicationInitializer
您可以通过链接找到更多信息: http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/WebApplicationInitializer。 html

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

这篇关于春季4杰克逊PrettyPrint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆