3

我正在尝试一些非常基本的网络服务。每次我尝试返回 Prtnr 对象时都会收到此异常。

Uncaught exception thrown in one of the service methods of the servlet: spitter. Exception thrown : 
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) 
(through reference chain: org.hibernate.collection.PersistentSet[0]->org.abc.dvo.PrtnrGeoInfo["id"]->org.abc.dvo.PrtnrGeoInfoId["partner"]->
org.abc.dvo.Prtnr["prtnrGeoInfos"]->org.hibernate.collection.PersistentSet[0]->org.abc.dvo.PrtnrGeoInfo["id"]->org.abc.dvo.PrtnrGeoInfoId["partner"]->
org.abc.dvo.Prtnr["prtnrGeoInfos"]->org.hibernate.collection.PersistentSet[0]->org.abc.dvo.PrtnrGeoInfo["id"]->org.abc.dvo.PrtnrGeoInfoId["partner"]->
org.abc.dvo.Prtnr["prtnrGeoInfos"]->org.hibernate.collection.PersistentSet[0]->org.abc.dvo.PrtnrGeoInfo["id"]->org.abc.dvo.PrtnrGeoInfoId["partner"]->
...
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:164)
    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)
    ...

Prtnr 类是:

public class Prtnr implements Cloneable, java.io.Serializable {

    private static final long serialVersionUID = 201207021420600052L;
    private Integer prtnrId;
    private String creatUserId;
    private Date creatTs;
    private String updtUserId;
    private Date updtTs;
    private String prtnrNm;
    private Integer cncilNum;
    private Character prtnrTypCd;
    private Set<PrtnrGeoInfo> prtnrGeoInfos = new HashSet<PrtnrGeoInfo>(0);
    private Set<PrtnrDtl> prtnrDtls = new HashSet<PrtnrDtl>(0);
    private Set<SuplyDtl> suplyDtls = new HashSet<SuplyDtl>(0);
    private Set<TrnsprtDtl> trnsprtDtls = new HashSet<TrnsprtDtl>(0);
    private Set<PrtnrFacil> prtnrFacils = new HashSet<PrtnrFacil>(0);
    private Set<PrtnrHumanResrc> prtnrHumanResrcs = new HashSet<PrtnrHumanResrc>(0);
    .....
    .....
    Getters and setters for these properties
    ...
}

PrtnrGeoInfo 类是:

public class PrtnrGeoInfo implements java.io.Serializable {
    private PrtnrGeoInfoId id = new PrtnrGeoInfoId();
    private String creatUserId;
    private Date creatTs;
    private String updtUserId;
    private Date updtTs;

    Getters and setters for these properties

}

PrtnrGeoInfoId 类是:

public class PrtnrGeoInfoId implements java.io.Serializable {   
    private Prtnr partner;
    private GeoSegment geoSegment;
    private static final long serialVersionUID = 201207060857580050L;

    Getters and setters for these properties
}

我相信这是因为这些类相互引用。但是这个问题怎么解决。在 Struts 2 和 Spring 的应用程序中,这个对象可以很好地通过。

控制器类如下:

@Controller
@RequestMapping("/partners")
public class PartnerController {
    @RequestMapping(value="/{id}", method=RequestMethod.GET, headers ={"Accept=text/xml,application/json"})
    @ResponseBody
    public Prtnr getPartner(@PathVariable("id") String id) throws Exception{
        Prtnr partner = null;
        try{
            partner = partnerService.getPartnerById(Integer.valueOf(id));
                System.out.println("******* Test message " );
        }catch(Exception ex){
            System.out.println("******* Exception thrown ... " + ex.getMessage());
        }

        return partner;
    }
}

调用类是 public class TestTemplate {

    private static final long serialVersionUID = 1130201273334264152L;
    public static void main(String[] args){
        Prtnr partner = (Prtnr)new RestTemplate().getForObject("http://localhost:9080/respondersApp/testWs/partners/{id}", Prtnr.class, "1");
        System.out.println("partner name is : " + partner.getPrtnrNm());
    }
}
4

4 回答 4

6

在此链接中,您可以找到解决此问题的方法。

但是下面我将在实践中粘贴解决方案。

这很简单。假设您的数据库查询已经在没有 JSON 的情况下工作,您所要做的就是:

@JsonManagedReferenc在关系的forward部分添加e(即User.java类):

@Entity
public class User implements java.io.Serializable{

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private long id;

 @Column(name="name")
 private String name;

 @ManyToMany
 @JoinTable(name="users_roles",joinColumns=@JoinColumn(name = "user_fk"),
 inverseJoinColumns=@JoinColumn(name = "role_fk"))
 @JsonManagedReference
 private Set<Role> roles = new HashSet<Role>();

...

@JsonBackReference在关系的后面部分添加(即 Role.java 类):

@Entity
public class Role implements java.io.Serializable {

 @Id 
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private int id;

 @ManyToMany(mappedBy="roles")
 @JsonBackReference
 private Set<User> users = new HashSet<User>();

...

工作完成。如果您查看您的萤火虫日志,您会注意到无限递归循环已经消失。

于 2015-03-29T17:21:01.073 回答
3

当您尝试将实体类转换为 JSON 格式时,这对我来说很常见。最简单的解决方案就是使用@JsonIgnore反向映射来打破循环。

于 2013-07-02T09:12:05.107 回答
2

您可以在 PrtnrGeoInfoId 中注释 Prtnr 的第二个引用@JsonBackReference

于 2014-03-01T00:08:30.320 回答
-1

无限递归是由于以下原因:类Prtnr包含Set<PrtnrGeoInfo> prtnrGeoInfos,每个PrtnrGeoInfo包含,而每个包含PrtnrGeoInfoId id又包含Prtnr partner

因此,Prtnr-> PrtnrGeoInfo-> PrtnrGeoInfoId->Prtnr会导致循环依赖,这对 Jackson 在尝试执行 POJO 映射时是一个问题。

您需要删除此循环依赖项才能修复此异常。

于 2013-01-11T15:46:42.437 回答