简体   繁体   English

春季启动-使用@ControllerAdvice处理HttpClientErrorException

[英]SPRING BOOT - Handle HttpClientErrorException using @ControllerAdvice

I have a Spring Boot Java application. 我有一个Spring Boot Java应用程序。 There is a service class that throws a "401 Unauthorized" HttpClientErrorException since the access token used in the application has expired. 由于在应用程序中使用的访问令牌已过期,因此服务类将引发“ 401未经授权”的HttpClientErrorException。 I want to handle this exception globally for which I have used the @ControllerAdvice annotation. 我想全局处理该异常,为此我已使用@ControllerAdvice批注。 The error is: 错误是:

Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized 引起原因:org.springframework.web.client.HttpClientErrorException:401未经授权

The class is: 该类是:

@Slf4j
@EnableWebMvc
@ControllerAdvice(basePackages = Service.class)
public class HttpClientErrorHandler{
    @ExceptionHandler(HttpClientErrorException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public String errorHandle(HttpClientErrorException e) {
        log.error("log HttpClientErrorException: ", e);
    return "HttpClientErrorException_message";
   }
}

Since the exception was caused in the service class, I have mentioned it specifically in the basePackages. 由于异常是在服务类中引起的,因此我在basePackages中特别提到了它。 The entire configuration for the program is specified in the application.yml file. 该程序的整个配置在application.yml文件中指定。 I have not used the xml configuration. 我没有使用过xml配置。 I don't understand why the @ControllerAdvice annotation is not working. 我不明白为什么@ControllerAdvice注释不起作用。 The program still throws the exception. 该程序仍会引发异常。 Can someone explain? 有人可以解释吗?

@ControllerAdvice(basePackages = Service.class)

The exception is bubbled to @Controller class, and @ControllerAdvice is supposed to apply to controller, so you should set basePackageClasses to your controller package instead of your service package. 异常冒泡到@Controller类,并且@ControllerAdvice应该适用于控制器,因此您应该将basePackageClasses设置为控制器软件包而不是服务软件包。

By default, @ControllerAdvice is applied to all Controller so you can remove the basePackageClasses unless you want to narrow down the controller advise 默认情况下, @ControllerAdvice ControllerAdvice应用于所有Controller,因此您可以删除basePackageClasses除非您想缩小控制器的建议范围。

I had also faced similar issue, 我也遇到过类似的问题,

try adding @Order(Ordered.HIGHEST_PRECEDENCE) below @ControllerAdvice . 尝试在@ControllerAdvice添加@Order(Ordered.HIGHEST_PRECEDENCE)

We add it to get priority over Spring's default DefaultHandlerExceptionResolver . 我们添加它以获得优先于Spring的默认DefaultHandlerExceptionResolver To understand more about why we add it read this answer . 要了解更多关于我们为什么添加它的信息,请阅读此答案 Also no need to give base packages, it will consider all packages by default. 同样也无需提供基本软件包,默认情况下它将考虑所有软件包。

To handle exception of any other type you can include below existing exception handler for HttpClientErrorException you already have written, 要处理任何其他类型的异常,您可以在已经编写的HttpClientErrorException的现有异常处理程序下面包括它,

@ExceptionHandler(Exception.class)
public Strring handleAnyExceptions(Exception ex) {
    return "your message";
} 

Hope it helps ! 希望能帮助到你 !

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM