缺少资源 bean:Springboot web-flux 自定义全局异常处理程序
Missing Resources bean : Springboot web-flux Custom Global Exception handler
我正在尝试通过扩展 AbstractErrorWebExceptionHandler(默认实现是 DefaultErrorWebExceptionHandler class)来实现我的自定义 GlobalExceptionHandler class,但无法这样做,因为缺少 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
我不确定为什么会这样。请帮忙!
@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));
}
}
兄弟。对不起我的英语不好。 Y 可以注入 WebProperties。下一步您可以从此属性执行 getResources()。希望对你有所帮助。
当我将我的 Webflux api 应用程序从 Springboot 版本 2.5.x 迁移到 2.6.2.
时,我遇到了同样的异常
为了解决它,我添加了一个配置 class,它为 WebProperties.Resources
创建了一个 Bean,如下所示,
@Configuration
public class ResourceWebPropertiesConfig {
@Bean
public WebProperties.Resources resources() {
return new WebProperties.Resources();
}
}
这解决了问题。我想 Springboot 版本 2 发生了一些变化。6.x
问题的根源在于
ResourceProperties(绑定到 spring.resources 的属性)在 Spring Boot 2.6(自 2.4 起已被弃用)中被删除,如 here and also in the said version release notes.
所述
在您的构造函数中注入 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);
}
我正在尝试通过扩展 AbstractErrorWebExceptionHandler(默认实现是 DefaultErrorWebExceptionHandler class)来实现我的自定义 GlobalExceptionHandler class,但无法这样做,因为缺少 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
我不确定为什么会这样。请帮忙!
@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));
}
}
兄弟。对不起我的英语不好。 Y 可以注入 WebProperties。下一步您可以从此属性执行 getResources()。希望对你有所帮助。
当我将我的 Webflux api 应用程序从 Springboot 版本 2.5.x 迁移到 2.6.2.
时,我遇到了同样的异常为了解决它,我添加了一个配置 class,它为 WebProperties.Resources
创建了一个 Bean,如下所示,
@Configuration
public class ResourceWebPropertiesConfig {
@Bean
public WebProperties.Resources resources() {
return new WebProperties.Resources();
}
}
这解决了问题。我想 Springboot 版本 2 发生了一些变化。6.x
问题的根源在于 ResourceProperties(绑定到 spring.resources 的属性)在 Spring Boot 2.6(自 2.4 起已被弃用)中被删除,如 here and also in the said version release notes.
所述在您的构造函数中注入 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);
}