java 自定义AbstractErrorWebExceptionHandler缺少WebProperties$资源Bean

busg9geu  于 2023-02-18  发布在  Java
关注(0)|答案(4)|浏览(367)

我尝试通过扩展AbstractErrorWebExceptionHandler(默认实现是DefaultErrorWebExceptionHandler类)来实现我的自定义GlobalExceptionHandler类,但无法实现,因为缺少构造器初始化所需的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));
    }
}

我得到的错误是:

***************************
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

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

zy1mlcev

zy1mlcev1#

当我将Webflux API应用程序从Springboot版本2.5.x迁移到2.6.2时,也遇到了同样的异常。
为了解决这个问题,我添加了一个配置类,为WebProperties.Resources创建一个Bean,如下所示:

@Configuration
public class ResourceWebPropertiesConfig {

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

}

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

bakd9h0s

bakd9h0s2#

问题的根源是在Sping Boot 2.6(自2.4起已弃用)中删除了ResourceProperties(绑定到spring.resources的属性),如here和所述版本发布说明中所述。
在构造函数中注入WebProperties bean,然后在使用点调用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);
    }
fhg3lkii

fhg3lkii3#

兄弟。对不起我的英语。Y可以注入WebProperties。下一步Y可以从这个属性做getResources()。希望我能帮到你。

sbtkgmzw

sbtkgmzw4#

@Slf4j
@Component
@Order(-99)
public class ExceptionHandler implements WebExceptionHandler {
    @Override
    public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
        ServerHttpResponse response = serverWebExchange.getResponse();
        response.setStatusCode(HttpStatus.BAD_REQUEST);
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        JSONObject resMsg = new JSONObject();
        try {
            resMsg.put("code", HttpStatus.BAD_REQUEST.value());
            if(throwable instanceof CommonException){
                resMsg.put("msg", ((CommonException) throwable).getMsg());
            }else{
                log.error("system error:", throwable);
                resMsg.put("msg", CommonCode.PLATFORM_ERR_MSG);
            }
        } catch (Exception e) {
        }

        DataBuffer db = response.bufferFactory().wrap(resMsg.toString().getBytes(Charset.forName("UTF-8")));
        return response.writeWith(Mono.just(db));
    }
}

相关问题