3

私はいくつかの非常に基本的なWebサービスを試しています。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;
    }
}

呼び出し元のクラスはパブリッククラス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

3

これは、エンティティ クラスを JSON 形式に変換しようとする場合によくあるシナリオです。最も簡単な解決策は@JsonIgnore、リバース マッピングで使用してサイクルを断ち切ることです。

于 2013-07-02T09:12:05.107 に答える
2

PrtnrGeoInfoId の Prtnr の 2 番目の参照に注釈を付けることができます。@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 に答える