`

解决spring mvc JSON 无法转换对象为json格式 无限死循环

阅读更多
控制台出现如下循环异常:
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at 
org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
	at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
	at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
	at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
	at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)

今天做显示商品评论功能  使用springmvc ajax加载,查询出来的数据转换成json时出现了死循环,如下:
引用

[{"commentid":22,"goods":{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产","goodses":[{"goodsid":8,"goodsarea":{"areaid":3,"areaName":"毕节特产",

查询评论表  而评论表和商品表是多对一的关系,商品表和分类表.地区表多对一的关系,
死循环原因:json无法对comments表的goods属性进行转换
解决办法:
在comments实体类上加注解@JsonIgnoreProperties(value={"goods"})
@JsonIgnoreProperties(value={"goods"})
public class Comments implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 6666646538881797793L;
	private Integer commentid;
	private Goods goods;
	private User user;
	private String comment;
	private String time;


分享到:
评论

相关推荐

    解决spring mvc JSON 无限死循环

    在开发基于Spring MVC的应用程序时,经常会遇到将对象转换为JSON格式的需求。然而,在某些情况下,由于对象之间的循环引用,可能会导致JSON序列化过程中出现无限递归的问题,进而引发StackOverflowError异常。本文将...

    SpringMVC中后台转换json格式

    1. `jackson-databind-2.7.0.jar`: 这是Jackson的主要模块,提供了对象映射功能,使得Java对象可以被转换为JSON格式,反之亦然。 2. `jackson-core-2.7.0.jar`: Jackson的核心模块,包含了基本的JSON解析和生成功能...

    spring mvc json&&jackson jquery js

    Spring MVC中的`@ResponseBody`注解可以将方法的返回值直接转换为JSON格式发送到客户端,而`@RequestBody`则可以将请求体中的JSON数据解析成Java对象。 **Jackson** Jackson是Java中广泛使用的JSON库,它可以高效地...

    spring mvc(整合了json)

    这里的 `@RequestBody` 用于将前端发送的 JSON 数据转换为 `User` 对象,而 `@ResponseBody` 则将方法的返回值转化为 JSON 响应给客户端。 5. **URL 映射和视图解析**:在 Spring MVC 中,我们通常通过 `@...

    Spring mvc Json处理实现流程代码实例

    为了返回JSON响应,只需在控制器方法中返回Java对象,Spring MVC会自动调用合适的HttpMessageConverter(如`MappingJackson2HttpMessageConverter`)将对象转换成JSON字符串,并设置响应内容类型为`application/json...

    Spring MVC 学习笔记 九 json格式的输入和输出

    在Spring MVC中,我们可以使用Jackson库或者Gson库将Java对象转换为JSON字符串,反之亦然。 2. **Spring MVC中的JSON配置** 在Spring MVC项目中,我们需要引入相应的JSON库,如Jackson的`jackson-databind`依赖。...

    spring mvc json学习

    为了将Java对象转换为JSON,我们需要引入Jackson库,这是一个流行的JSON处理库。在项目中添加Jackson依赖后,Spring MVC会自动配置并使用它来序列化和反序列化Java对象。 在控制器(Controller)中,我们可以使用`@...

    spring4 mvc json配置jar包

    这些库提供了强大的JSON解析和生成功能,使得Java对象可以方便地转换为JSON格式和从JSON字符串还原。 1. **Jackson库简介** Jackson是Java领域最流行的JSON处理库之一,由 FasterXML 维护。它包括三个主要模块:`...

    四. Spring MVC 3 支持json及 常见错误解决

    在Spring MVC中,JSON主要用于将Java对象转换为JSON字符串,然后通过HTTP响应发送给客户端,或者将客户端发送的JSON数据转换为Java对象。 **一、Spring MVC 3中的JSON支持** 1. **配置依赖库**:为了处理JSON,你...

    spring-mvc-jsonview源代码

    当返回类型为@RequestBody或@ResponseBody时,Spring MVC会自动将返回的对象转换为JSON,发送到客户端。 三、源代码分析 1. pom.xml:项目依赖管理文件,包含了Spring MVC、Jackson库和其他相关依赖。例如,添加...

    spring MVC 对 ResponseBody返回json数据进行脱敏处理

    在Spring MVC框架中,开发Web应用时经常需要将对象转换为JSON格式的数据并返回给客户端。然而,在处理敏感数据时,我们可能希望对这些数据进行脱敏,即隐藏或替换某些字段,以保护用户隐私或者确保数据安全性。本文...

    Spring3 MVC Ajax with JSON

    在Ajax请求中,JSON是传输数据的理想选择,因为它可以被JavaScript直接解析为对象,减少了数据处理的复杂性。 ### 5. 整合步骤 1. **配置Spring MVC**:在`DispatcherServlet`的配置文件中启用注解驱动的MVC,可能...

    spring mvc生成xml或json

    Spring MVC会自动使用Jackson库将`User`对象转换为JSON格式。 2. **生成XML** 对于XML,Spring默认使用JAXB(Java Architecture for XML Binding)库。首先,确保项目包含JAXB库,如果使用的是Java 6或更高版本,...

    spring mvc返回json几个包 jackson1.9.13

    总有4个包 jackson-core-asl-1.9.13.jar jackson-core-lgpl-1.9.13 jackson-mapper-asl-1.9.13 jackson-mapper-lgpl-1.9.13

    spring mvc拦截器过滤json中的null值

    // 将JSON字符串转换为对象 ObjectMapper mapper = new ObjectMapper(); Object jsonObject = mapper.readValue(content.toString(), Object.class); // 过滤并替换null值 jsonObject = filterNullValues...

    Spring MVC 数据转换和格式化.docx

    一旦设置好,我们就可以通过在控制器的方法上添加`@ResponseBody`注解,指示Spring MVC将方法返回的对象转换为JSON并返回给客户端。例如,如示例代码所示,当调用`getRole3`方法时,返回的`Role`对象会被自动转换为...

    springmvc操作json数据所需的jar包

    2. **Jackson-databind**: 提供了与Java Bean对象映射的功能,可以方便地将Java对象转换为JSON,反之亦然。 3. **Jackson-annotations**: 包含了用于注解Java类以控制序列化和反序列化的注解,如`@JsonProperty`、`@...

    spring-json

    "Spring-JSON"是关于Spring框架与JSON处理的相关知识点,主要涉及如何在Spring应用程序中集成和使用JSON数据。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于前后端交互。Spring框架提供...

    用Spring MVC 搭建JSON 数据服务器(二)

    在本教程中,我们将深入探讨如何使用Spring MVC框架构建一个返回JSON格式数据的服务器。首先,这个项目涉及的技术栈包括Spring MVC、JDBC、JSON以及Gson。Spring MVC是Spring框架的一部分,它允许我们构建RESTful ...

    Spring MVC返回JSON数据 用到的jar包

    在Spring MVC中,为了将Java对象转换成JSON格式并返回给客户端,我们需要依赖于JSON库。在提供的信息中,提到了`jackson-all-1.9.x.jar`系列的包,这是Jackson库的一个早期版本。Jackson是一个非常流行的JSON处理库...

Global site tag (gtag.js) - Google Analytics