Spring MVC和Jackson映射不返回json中的根元素 [英] Spring MVC and Jackson mapping do not return the root element in json

查看:149
本文介绍了Spring MVC和Jackson映射不返回json中的根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Spring MVC及其json支持的一个问题。我做一个ajax调用来获取一些数据,我想以json格式获取包含根值的数据。我还在实体中使用 JABX 注释,因为它们用于某些 REST API

I am having one issue with Spring MVC and its json support. I make one ajax call to get some data and I want to get that data in json format including the root value. I am also using JABX annotations in the entities because those are used for some REST API.

我已经读过,为了获得 Jackson 中包含的根值,我应该使用这种方法:

I have read that to get the root value included with Jackson, I should use this method:

this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

所以我创建了一个objectMapper,它扩展了codehaus一个,看起来像这样:

So I created one objectMapper which extends the codehaus one and looks like this:

public class JaxbJacksonObjectMapper extends ObjectMapper {

    public JaxbJacksonObjectMapper() {
        final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

        this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
        super.getDeserializationConfig().withAnnotationIntrospector(introspector);

        this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        super.getSerializationConfig().withAnnotationIntrospector(introspector);
    }
}

对于Spring使用此映射器我已经配置了以下内容行:

For Spring to use this mapper I have configured the following lines:

<beans:bean id="customObjectMapper" class="com.test.package.config.JaxbJacksonObjectMapper" />

<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <beans:property name="objectMapper" ref="customObjectMapper" />
</beans:bean>

<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>

我的实体看起来像这样:

And my entities look like this:

@XmlRootElement(name = "collection")
public class Issuers {
    private List<Issuer> issuers;
}

问题是当 Spring 3.1 将问题者json对象返回给浏览器,它不包含集合根元素。

The problem is that when Spring 3.1 returns the Issuers json object to the browser it does not include the collection root element.

任何想法如何解决这个问题?

Any idea how to solve this issue?

谢谢!

推荐答案

似乎方法 withAnnotiationIntrospector 未设置 AnnotiationIntrospector 。它返回新的DeserializationConfig / SerializationConfig 对象(正确的 AnnotiationIntrospector )。

It seems that the method withAnnotiationIntrospector does not set AnnotiationIntrospector. It's returns new DeserializationConfig/SerializationConfig object instead (with correct AnnotiationIntrospector).

所以,我的版本JaxbJacksonObjectMapper

public class JaxbJacksonObjectMapper extends ObjectMapper {

    public JaxbJacksonObjectMapper() {
        super();

        final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

        this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
        this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

        this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));
        this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));

    }
}

现在支持 @XmlRootElement @XmlTransient 等。

这篇关于Spring MVC和Jackson映射不返回json中的根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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