简体   繁体   English

即使服务器出错,ResponseBuilder 也会给出 200 OK HTTP 响应

[英]ResponseBuilder gives 200 OK HTTP response even for server error

I am using javax.ws.rs.core.Response api to build and send back response to front end from my spring boot backend.我正在使用 javax.ws.rs.core.Response api 来构建响应并将响应从我的 Spring Boot 后端发送回前端。 I have a controller as follows.我有一个控制器如下。

@GetMapping(value = "/details")
public Response getDetails() throws ServiceException {
    try {
        //logic to get details
        return Response.status(Response.Status.OK).entity(details).build();

    } catch (Exception e) {
        log.error(e.getMessage());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage).build();
    }

}

But I am getting 200 OK as the response status for the HTTP Request in the browser/postman even in the case of exceptions.但是即使在出现异常的情况下,我也得到 200 OK 作为浏览器/邮递员中 HTTP 请求的响应状态。 Because of this, the ajax success block executes instead of the error block when there is an exception.正因为如此,当出现异常时,ajax成功块而不是错误块被执行。 Is there any way to make the error block execute in this case?在这种情况下,有没有办法让错误块执行? I don't want to throw an exception as it sends the entire stack trace in the response.我不想抛出异常,因为它在响应中发送整个堆栈跟踪。

@GetMapping is from spring mvc and spring-mvc is not JAX-RS compliant. @GetMapping来自 spring mvc,而 spring-mvc 不符合 JAX-RS。 So don't mix to use them together.所以不要混合使用它们。 Either use pure spring-mvc or pure JAX-RS .使用纯 spring-mvc 或纯 JAX-RS 。 you can try to return a spring-mvc ResponseEntity (equivalent to JAX-RS Response ) instead:您可以尝试返回一个 spring-mvc ResponseEntity (相当于 JAX-RS Response ):

@GetMapping(value = "/details")
public ResponseEntity getDetails() throws ServiceException {
    try {
        //logic to get details
        return ResponseEntity.ok(details);

    } catch (Exception e) {
        log.error(e.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage);
    }

}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP 405-200 OK响应上的方法不允许错误 - HTTP 405 - Method Not Allowed error on a 200 OK response 即使输入了错误的密码并且文件未保存在服务器上,我也总是得到服务器响应正常200 - I get always Server Response ok 200 even if I give the wrong password and files are not saved on server 响应 200 ok 并转到错误行 - Response 200 ok and goes to the error line 为什么我的服务器响应状态代码是200而不是200 OK? - Why my server response status code is 200 instead of 200 ok? OpenRefine 的 API:意外响应:HTTP/1.1 200 ok - OpenRefine's API: Unexpected response: HTTP/1.1 200 ok Jersey HTTP响应200可以,但是返回内容错误(检查请求) - Jersey HTTP response 200 OK but returning content wrong (checking request) 为什么response.getStatus()返回200 OK,即使实际响应不是 - Why is response.getStatus() returning 200 OK even when the actual response is not 泽西Response.ok()不给200 OK - Jersey Response.ok() not giving 200 OK 来自服务器 [200] 的 HTTP 响应不允许 HTTP 升级到 WebSocket - The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket Spring 引导 GET 请求给出 200 OK 状态,但 Postman 返回“ø”作为响应主体 - Spring Boot GET Request gives 200 OK status, but Postman returns "ø" as response body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM