【问题标题】:WebProperties$Resources bean missing for custom AbstractErrorWebExceptionHandler缺少资源 bean:Spring Boot webflux 自定义全局异常处理程序
【发布时间】: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


【解决方案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));
    }
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

兄弟。对不起我的英语不好。 Y 可以注入 WebProperties。下一步 y 可以从此属性执行 getResourses() 。希望对你有所帮助。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 没问题兄弟,是的,你是对的! :)
【解决方案3】:

当我将我的 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 发生了一些变化

【讨论】:

  • 是的,你是对的! springboot 2.6.x 版本发生了一些变化。
猜你喜欢
  • 2022-10-19
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2018-01-04
  • 2010-10-17
  • 2011-04-07
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多