[英]Custom Http Status Code in Spring
I am using Spring Boot and I am using Exception Classes thoughout my business logic code.我正在使用 Spring Boot 并且在我的业务逻辑代码中使用异常类。 One might look like this:
一个可能看起来像这样:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class ExternalDependencyException extends RuntimeException {
public ExternalDependencyException() {
super("External Dependency Failed");
}
public ExternalDependencyException(String message) {
super(message);
}
}
Well now there are Exception, where no predefined Http Status code is fitting, so I would like to use a status code like 460 or similar, which is still free, but the annotation ResponseStatus
just accepts values from the enum HttpStatus
.那么现在有异常,没有预定义的 Http 状态代码适合,所以我想使用像 460 或类似的状态代码,它仍然是免费的,但注释
ResponseStatus
只接受来自枚举HttpStatus
值。 Is there a way to achieve an Exception class with a custom status code in the java spring boot environment?有没有办法在java spring boot环境中实现带有自定义状态码的Exception类?
I'm not aware of a way to do this with @ResponseStatus .我不知道有什么方法可以用@ResponseStatus做到这一点。
One way to solve is this issue is with @RestControllerAdvice .解决此问题的一种方法是使用@RestControllerAdvice 。 This will allow you to customize how you return your Exception.
这将允许您自定义返回异常的方式。
@RestControllerAdvice
public class WebRestControllerAdvice {
@ExceptionHandler(ExternalDependencyException.class)
public String handleGitlabException(ExternalDependencyException ex, HttpServletResponse response) {
try {
response.sendError(460);
} catch (Exception e) {
e.printStackTrace();
}
return ex.getMessage();
}
}
In general, @mdeterman answer is good, but I believe the right thing to do is setStatus to response, not sendError directly.一般来说,@mdeterman 的回答很好,但我相信正确的做法是 setStatus 来响应,而不是直接发送错误。 Example:
示例:
@RestControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(CustomException.class)
public String handleException(Exception ex, HttpServletResponse response) {
response.setStatus(460);
return ex.getMessage();
}
}
It seems to be possible only because of @RestControllerAdvice.似乎只有因为@RestControllerAdvice 才有可能。 @ControllerAdvice reset status code on view resolver level (for some strange reason).
@ControllerAdvice 在视图解析器级别重置状态代码(出于某种奇怪的原因)。 Also it is important to understand: any custom status code (not mentioned in org.apache.http.HttpStatus - RFC1945, RFC2616 , and RFC2518) will cause UnknownHttpStatusCodeException if client service is based on spring.
同样重要的是要了解:如果客户端服务基于 spring ,任何自定义状态代码(在 org.apache.http.HttpStatus - RFC1945、RFC2616 和 RFC2518 中未提及)都将导致 UnknownHttpStatusCodeException。
Yes, sendError()
will use Spring default Template.是的,
sendError()
将使用 Spring 默认模板。 You also need to use RestControllerAdvice
instead of ControllerAdvice
, then setStatus()
will do the trick :)您还需要使用
RestControllerAdvice
而不是ControllerAdvice
,然后setStatus()
就可以了:)
@RestControllerAdvice
public class ExceptionService {
@SneakyThrows
@ExceptionHandler({CustomException.class})
public CustomExceptionDto handle(HttpServletResponse resp, CustomException e) {
resp.setStatus(e.getStatus().value()); //Using Spring HttpStatus
return e.toDto();
}
}
In Spring Boot 2.1.8.RELEASE I did something similar but without using the HttpServletResponse:在 Spring Boot 2.1.8.RELEASE 中,我做了类似的事情,但没有使用 HttpServletResponse:
@RestControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(MyCustomException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public ApiErrorResponse handleTemplateEngineException(MyCustomException ex) {
//custom ApiErrorResponse class
return ApiErrorResponse.builder()
.status(HttpStatus.NOT_ACCEPTABLE)
.errorCode("NOT_ACCEPTABLE")
.message(ex.getLocalizedMessage()).build();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.