Spring Boot:错误:响应已提交后无法调用sendError() [英] Spring Boot : Error :Cannot call sendError() after the response has been committed

查看:636
本文介绍了Spring Boot:错误:响应已提交后无法调用sendError()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误. 在提交响应后无法调用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()

解决方案

这是双向关系的问题,因为它们相互引用,反序列化时,Jackson无限循环运行.我的第一个建议是在关系的一端添加@JsonIgnore.

@OneToOne(mappedBy = "details")
@JsonIgnore
private Product product;

然后,如果这解决了您的问题,则可以查看@ JsonManagedReference/@ JsonBackReference和@JsonIdentityInfo.

您也可以查看此链接以获得更多见识

I am getting this error . Cannot call sendError() after the response has been committed Can someone help me figure out why?.

    @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);
    }
}

I am able to make the POST call to /details; for adding details successfully. But when i make GET call to /getAllProducts, I am getting this error Cannot call sendError() after the response has been committed

解决方案

This is an issue with bidirectional relationships, as they hold references to each other, at deserialization, Jackson runs in an infinite loop. My first suggestion would be adding @JsonIgnore to one end of the relation.

@OneToOne(mappedBy = "details")
@JsonIgnore
private Product product;

Afterward, if that solved your issue, you can look over @JsonManagedReference/@JsonBackReference and @JsonIdentityInfo.

You can also look over this link for more insight

这篇关于Spring Boot:错误:响应已提交后无法调用sendError()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆