我收到此错误。 提交响应后无法调用sendError() 有人可以帮我找出原因吗?。
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
@JoinColumn(name = "details_id")
private Details details;
//Getters and setters left out for brevity
}
@Entity
public class Details {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String description;
private float price;
private float discount;
@OneToOne(mappedBy = "details")
private Product product;
}
@RestController
public class ProductController {
@Autowired
ProductRepository productRepository;
@GetMapping("/getAllProducts")
public Iterable<Product> getAllProducts(){
return productRepository.findAll();
}
}
@RestController
public class DetialsController {
@Autowired
ProductRepository productRepository;
@Autowired
DetailsRepository detailsRepository;
@PostMapping("/details")
public Details addDetails(@RequestBody Details details) {
Product newProduct = new Product();
newProduct.setDetails(details);
productRepository.save(newProduct);
return detailsRepository.save(details);
}
}
我可以对/ details进行POST调用;成功添加详细信息。但是当我对/ getAllProducts进行GET调用时,出现此错误 在提交响应后无法调用sendError()
答案 0 :(得分:3)
这是双向关系的问题,因为它们相互引用,反序列化时,Jackson无限循环运行。我的第一个建议是在关系的一端添加@JsonIgnore
。
@OneToOne(mappedBy = "details")
@JsonIgnore
private Product product;
然后,如果这解决了您的问题,则可以查看@ JsonManagedReference / @ JsonBackReference和@JsonIdentityInfo。
您也可以查看此link以获得更多见识