Jakson IllegalArgumentException:无法处理托管/后退引用

时间:2017-06-14 23:29:09

标签: java web-services jackson spring-rest

上下文:

我有2个webapps:

  • front(spring mvc)
  • webservice(春季休息)

都通过webservice进行通信。我正试图在json的webservice webapp中生成一个objet(CounterDTO),如下所示:

  

ObjectMapper mapper = new ObjectMapper();   mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,   假); CounterDTO counterDTO =   mapper.readValue(json.get(objName).toString(),CounterDTO.class);

错误:

当我尝试用json构建一个objet时,我有这个错误:

  

java.lang.IllegalArgumentException:无法处理托管/返回   引用'entreprise-agency':后引用类型(java.util.Set)没有   与托管类型兼容(com.mypackage.Agency)

发送JSON:

  

{“id”:null,“code”:“SKYUE586”,“name”:“name 1”}

我有3个这样的实体:

@Entity
@Table(name = "AGENCY")
public class Agency {
    ...
    @ManyToOne(optional = false)
    @JoinColumn(name = "ID_Entreprise")
    @JsonManagedReference(value = "entreprise-agency")
    private Entreprise entreprise;

    @OneToMany(mappedBy = "agency", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonBackReference(value = "agency-counter")
    private Set<Counter> counters = new HashSet<Counter>();
   ...
}

Entity
@Table(name = "COUNTER")
public class Counter {
    ...
    @ManyToOne(optional = false)
    @JoinColumn(name = "ID_Agence")
    // @JsonBackReference
    @JsonManagedReference(value = "agency-counter")
    private Agency agency;
    ...
}

@Entity
@Table(name = "ENTREPRISE")
public class Entreprise {
    ...
    @OneToMany(mappedBy = "entreprise", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonBackReference(value = "entreprise-agency")
    private Set<Agency> agencies = new HashSet<Agency>();
   ...
}

和DTO

public class CounterDTO {
    private Integer id;
    private String code;
    private String name;
    ...
}

问题:

如何正确处理托管/后退杰克逊参考?

我也尝试在我的实体上使用此标记

  

@JsonIdentityInfo(generator =   ObjectIdGenerators.PropertyGenerator.class,property =“id”)

由于

1 个答案:

答案 0 :(得分:1)

我已使用

解决了问题

Gson library

而不是ObjectMapper