CONTEXT:
我有 2 个网络应用程序:
- 前(弹簧MVC)
- 网络服务(春休)
两者都通过网络服务进行通信。我正在尝试从 json 在网络服务网络应用程序中生成一个对象(反DTO),如下所示:
ObjectMappermapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 假);计数器 DTO 计数器 DTO = mapper.readValue(json.get(objName).toString(), CounterDTO.class);
错误:
当我尝试使用 json 构建一个对象时,出现此错误:
java.lang.IllegalArgumentException: 无法处理托管/返回 reference 'entreprise-agency': back reference type (java.util.Set) not 与托管类型(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(生成器 = ObjectIdGenerators.PropertyGenerator.class, property = "id")
谢谢
我已经通过使用解决了问题
格森图书馆
而不是 ObjectMapper