【发布时间】:2022-01-15 03:47:19
【问题描述】:
我正在尝试通过扩展 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));
}
}
我得到的错误是:
***************************
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
我不确定为什么会这样。请帮忙!
【问题讨论】:
-
你的班级需要一个没有提供的
Resources resources,没有看到更多你的应用程序和你的配置,这是不可能回答的
标签: java spring-boot spring-webflux