春季REST | MappingJacksonHttpMessageConverter产生无效的JSON [英] Spring REST | MappingJacksonHttpMessageConverter produces invalid JSON

查看:109
本文介绍了春季REST | MappingJacksonHttpMessageConverter产生无效的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Spring实现了RESTful Web服务.服务根据接受标头以XML或JSON进行响应.这是context.xml映射:

I've implemented a RESTful web service with Spring. The service responds in XML or JSON based on the Accept header. Here's the context.xml mapping:

  <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
  <bean id="xmlMessageConverter"
        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="xstreamMarshaller"/>
    <property name="supportedMediaTypes" value="application/xml"/>
  </bean>

  <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="prefixJson" value="false"/>
    <property name="supportedMediaTypes" value="application/json"/>
  </bean>

  <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <util:list id="beanList">
        <ref bean="xmlMessageConverter"/>
        <ref bean="jsonHttpMessageConverter"/>
      </util:list>
    </property>
  </bean>

这是我的控制器方法:

@Controller
@RequestMapping(value = "/entityService")
class RestfulEntityService {

  @Resource
  private EntityService entityService;

  @ResponseBody
  @RequestMapping(value = "/getAllEntities", method = RequestMethod.GET)
  public List<Entity> getAllEntities() {
    return entityService.getAllEntities();
  }
}

XML响应是有效的,但是,当客户端将Accept标头设置为application/json时,该响应是无效的JSON.

The XML response is valid, however, when the client sets the Accept header to application/json, the response is invalid JSON.

这是JSON响应示例:

Here is the JSON response sample:

[{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes": ..... repeats for a while and then stops..

推荐答案

您正在使用 XStream 进行序列化XML响应和 Jackson JSON 来序列化JSON响应.查看您发布的JSON输出,似乎手头有一个循环参考问题.我猜Entity有一个属性列表,每个属性都指向它们各自的实体. XStream通过使用XPath透明地处理循环引用,这允许在反序列化回对象时保留引用. Jackson自v1.6起就可以处理循环引用,但是您需要通过使用@JsonManagedReference@JsonBackReference注释序列化的实体来提供帮助.我认为Jackson在允许反向引用JSON序列化方面是独一无二的.

You're using XStream to serialize XML responses and Jackson JSON to serialize JSON responses. Looking at the JSON output you posted, it seems like there's a circular reference issue at hand. I'm guessing Entity has a list of attributes, each pointing to their respective entity. XStream handles circular references transparently by using XPath, this allows to preserve references when deserializing back to objects. Jackson is able to handle circular references since v1.6, but you need to help it by annotating your serialized entities with @JsonManagedReference and @JsonBackReference. I think Jackson is unique in allowing back references in JSON serialization.

有关使用声明性方法处理双向引用的信息,请参阅Jackson的文档.

这篇关于春季REST | MappingJacksonHttpMessageConverter产生无效的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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