[英]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.