我有这段代码:

 public HttpEntity<byte[]> getReport(){
        WebController.LOGGER.info("Requested report");

        try {
            byte[] reportData= reportService.getReport();
            return new HttpEntity<byte[]>(reportData);
        } catch (ReportGenerationException e) {
            //WHAT DO I RETURN HERE?
        }
    }

当无法生成报告时,我想返回500错误,但是HttpEntity不允许我设置HTTP状态代码。我该如何解决?

最佳答案

您可以使用ResponseEntity(HttpEntity的子级)来设置HTTP状态代码。

public ResponseEntity<byte[]> getReport(){
    WebController.LOGGER.info("Requested report");

    try {
        byte[] reportData= reportService.getReport();
        return new HttpEntity<byte[]>(reportData);
    } catch (ReportGenerationException e) {
        //WHAT DO I RETURN HERE?
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

关于java - 在Spring MVC中使用HttpEntity返回错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35451871/

10-09 00:17