Spring POST 关系 json 无效

Spring POST relationship json not working

我试图 post 一个与服务器有关系的对象。 但是我得到一个错误。

我的实体:

@Entity
public class District implements Serializable {

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

@Column(unique = true)
private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JsonUnwrapped
private City city;

@JsonManagedReference
@JsonIgnoreProperties
@OneToMany(mappedBy = "district", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Region> regions = new HashSet<>();

@JsonIgnoreProperties
@OneToMany(mappedBy = "district", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Drugstore> drugstores = new HashSet<>();

//getter and setters

这是我的控制器:

@PostMapping("/admin/district")
ResponseEntity<?> create(@RequestBody District request) {
    service.create(request);
    return null;
}

这是我的错误:

如果我发送 null json,我会再次收到此错误。 有人可以帮忙吗?

我尝试了几分钟后找到了解决方案。

If use @JsonBackReference in project more than twice, should distinguish them with a specific Reference name.

所以我喜欢这样:

@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference("district-city")
private City city;

@JsonManagedReference("district-region")
@JsonIgnoreProperties
@OneToMany(mappedBy = "district", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Region> regions = new HashSet<>();

@JsonIgnoreProperties
@JsonManagedReference("district-drugstore")
@OneToMany(mappedBy = "district", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Drugstore> drugstores = new HashSet<>();

解决方案已解决。

希望能帮到别人...