为了账号安全,请及时绑定邮箱和手机立即绑定

递归 JSON 输入导致 SyntaxError: Unexpected end of JSON

递归 JSON 输入导致 SyntaxError: Unexpected end of JSON

富国沪深 2023-10-12 16:53:12
我已经花了几个小时了,我似乎可以解决这个问题。我有两个实体产品和客户,其中一个客户可以拥有一种产品,而一个产品可能有多个客户。在我的 SQL SERVER Management studio 中,Product 表的主键作为 Customer 表中的外键。我在下面的代码中展示了这两个实体。问题在于,客户“c”被递归地附加到“myproducts”,这是当我在浏览器窗口上检查控制台时显示的 JSON 中的mappedBy 属性。(请参阅下面错误中的嵌套对象“myproducts”和“c”)我正在使用 GET 方法 API 在屏幕上显示客户。Products.java@Entity@Table(name="NewProductDetails")public class Products{    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "p_id")    private int productId;    @Size(max=65)    @Column(name = "p_name")    private String name;    @Column(name = "p_price")    private int price;    @OneToMany(fetch = FetchType.LAZY,              cascade = CascadeType.ALL,              mappedBy = "myproduct")    public  Set<Customer> c;    public Products() {    }    public Products(String p_name, int p_price) {        this.name = p_name;        this.price = p_price;    }    public long getproductId() {        return productId;    }    public void setproductId(int id) {        this.productId = id;    }    public void setPName(String p_name) {        this.name = p_name;    }    public String getPName() {        return this.name;    }    public void setPrice(int p_price ) {        this.price  = p_price ;    }    public int getPrice() {        return this.price;    }}ProductController.java@CrossOrigin(origins = "http://localhost:4200")@RestController@RequestMapping("/api")public class ProductController {    @Autowired    ProductRepository productRepository;    @GetMapping("/product")    public List<Products> getAllProducts(){        System.out.println("Get All the product .... ");        List<Products> products = new ArrayList<>();        productRepository.findAll().forEach(products :: add);        return products;    }
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

在每个实体(产品和客户)中使用@JsonIgnoreProperties()解决了错误,并且循环依赖消失了。


查看完整回答
反对 回复 2023-10-12
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信