Java Spring REST | MappingJacksonHttpMessageConverter生成无效的JSON

Java Spring REST | MappingJacksonHttpMessageConverter生成无效的JSON,java,json,spring,rest,Java,Json,Spring,Rest,我已经用Spring实现了一个RESTful web服务。服务根据Accept头以XML或JSON响应。下面是context.xml映射: <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/> <bean id="xmlMessageConverter" class="org.springframework.http.convert

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


请参阅上的Jackson文档以供参考。

可能值得发布完全有效和完全无效的响应。有效响应非常大(有效响应也是如此),因为它必须序列化100个实体的列表。我发布的无效响应被重复了一百次并停止..有趣的是,一个包含一个或两个实体的小列表被正确地序列化为JSON..听起来很有希望。。让我试试!
@Controller
@RequestMapping(value = "/entityService")
class RestfulEntityService {

  @Resource
  private EntityService entityService;

  @ResponseBody
  @RequestMapping(value = "/getAllEntities", method = RequestMethod.GET)
  public List<Entity> getAllEntities() {
    return entityService.getAllEntities();
  }
}
[{"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..