我正在尝试显示JSON数据,因为我使用的是fastxml jackson。我能够显示JSON,但JSON正在进入无限循环,在日志文件中我得到错误
ERROR [org.apache.catalina.core.ContainerBase [jboss.web] [缺省主机]。[/ EAD]。[弹簧]] (http - 127.0.0.1-8080-1)用于servlet spring的Servlet.service()抛出 exception:java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonMappingException $ Reference from [模块“deployment.EAD.war:main”来自服务模块加载器]
这是我的控制器方法。
@RequestMapping("/viewDashboard")
@ResponseBody
public ModelMap test(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestParam String name) {
ModelMap modelMap = new ModelMap();
List<Instances> instances = viewDashboardServiceImpl.viewDashboard();
return modelMap.addAllAttributes(instances);
}
实际上我的Instance.java与Application.java和Environment.java有关系
以下是我的model.java
这是我的instance.java
@Id
@GeneratedValue
@Column(name = "instanceId")
private long id;
@Column(name = "name")
private String instanceName;
@ManyToOne
@JoinColumn(name = "appId")
private Applications applications;
@ManyToOne
@JoinColumn(name = "enviId")
private Environments environments;
@Column(name = "version")
private String version;
这是我的Application.java
@Id
@GeneratedValue
@Column(name = "appId")
private long id;
@Column(name = "name")
private String name;
@Column(name = "ownerName")
private String ownerName;
@Column(name = "version")
private String version;
@OneToMany(mappedBy = "applications", targetEntity = Instances.class)
private Set<Instances> instances;
这是我的Environment.java
@Id
@GeneratedValue
@Column(name = "enviId")
private long id;
@Column(name = "name")
private String name;
@Column(name = "ownerName")
private String ownerName;
@Column(name = "version")
private String version;
@OneToMany(mappedBy = "environments", targetEntity = Instances.class)
private Set<Instances> instances;
这是我的JSON
{"instances":{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":{"id":1,"name":"SAP","ownerName":"Mou","version":"3.2.1abc","instances":[{"id":2,"instanceName":"SAP BW DEV1","applications":
如上所述JSON继续重复数据。 我在maven中也有以下依赖。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.5</version>
</dependency>
我还在servlet.xml中添加了以下代码段。
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>