猿问

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

我已经花了几个小时了,我似乎可以解决这个问题。我有两个实体产品和客户,其中一个客户可以拥有一种产品,而一个产品可能有多个客户。在我的 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;

    }



富国沪深
浏览 90回答 1
1回答

慕妹3242003

在每个实体(产品和客户)中使用@JsonIgnoreProperties()解决了错误,并且循环依赖消失了。
随时随地看视频慕课网APP

相关分类

Java
我要回答