自定义 AbstractErrorWebExceptionHandler 缺少 WebProperties$Resources bean

Ris*_*ngh 9 java spring-boot spring-webflux

我试图通过扩展 AbstractErrorWebExceptionHandler 来实现我的自定义 GlobalExceptionHandler 类(默认实现是 DefaultErrorWebExceptionHandler 类),但无法这样做,因为缺少构造函数初始化所需的 bean(如下所述)。我不确定为什么默认情况下会发生这种情况实现工作正常,通过提供我自己的实现,它要求一个 bean,请帮忙

@Component
@Order(-2)
public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler{

    public GlobalExceptionHandler(ErrorAttributes errorAttributes, Resources resources, ApplicationContext applicationContext) {
        super(errorAttributes, resources, applicationContext);
    }

    @Override
    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
        return RouterFunctions.route(RequestPredicates.all(),this::formatErrorResponse);
    }

    private Mono<ServerResponse> formatErrorResponse(ServerRequest request){
        Map<String, Object> errorAttributesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults());
        int status = (int) Optional.ofNullable(errorAttributesMap.get("status")).orElse(500);
        return ServerResponse
                .status(status)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(errorAttributesMap));
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.example.userManagementSystem.demoApp.exception.GlobalExceptionHandler required a bean of type 'org.springframework.boot.autoconfigure.web.WebProperties$Resources' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.web.WebProperties$Resources' in your configuration.


Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样。请帮忙!

Bal*_*ala 14

当我将 Webflux api 应用程序从 Springboot 版本 2.5.x 迁移到 2.6.2 时,我遇到了同样的异常。

为了解决这个问题,我添加了一个配置类,它创建了一个 Bean,WebProperties.Resources如下所示,

@Configuration
public class ResourceWebPropertiesConfig {

    @Bean
    public WebProperties.Resources resources() {
        return new WebProperties.Resources();
    }

}
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题。我猜 Springboot 版本 2.6.x 中发生了一些变化


Lex*_*hor 12

问题的根源在于 ResourceProperties(即绑定到 spring.resources 的属性)在 Spring Boot 2.6 中被删除(自 2.4 起已弃用),如此处以及所述版本发行说明中所解释的。

在构造函数中注入WebPropertiesbean,然后WebProperties.getResources()在使用点调用应该可以修复自定义全局异常处理程序,如以下代码片段所示:

@Component
@Order(-2)
public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler{

    // Spring boot 2.5.x 
    public GlobalExceptionHandler(ErrorAttributes errorAttributes, Resources resources, ApplicationContext applicationContext) {
        super(errorAttributes, resources, applicationContext);
    }

    // Spring boot 2.6.0
    public GlobalExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties, ApplicationContext applicationContext) {
        super(errorAttributes, webProperties.getResources(), applicationContext);
    }
Run Code Online (Sandbox Code Playgroud)