java 春季休息 | MappingJacksonHttpMessageConverter 产生无效的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:54:41  来源:igfitidea点击:

Spring REST | MappingHymansonHttpMessageConverter produces invalid JSON

javajsonspringrest

提问by Sri

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:

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

  <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.MappingHymansonHttpMessageConverter">
    <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>

Here is my controller method:

这是我的控制器方法:

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

  @Resource
  private EntityService entityService;

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

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

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

Here is the JSON response sample:

这是 JSON 响应示例:

[{"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..

回答by Ophir Radnitz

You're using XStreamto serialize XML responses and Hymanson JSONto serialize JSON responses. Looking at the JSON output you posted, it seems like there's a circular reference issue at hand. I'm guessing Entityhas 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. Hymanson is able to handle circular references since v1.6, but you need to help it by annotating your serialized entities with @JsonManagedReferenceand @JsonBackReference. I think Hymanson is unique in allowing back references in JSON serialization.

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

See Hymanson's documentation on handling bi-directional references using declarative methodsfor reference.

请参阅Hyman逊关于使用声明性方法处理双向引用的文档以供参考。