繁体   English   中英

NoSuchBeanDefinitionException:没有可用的“org.springframework.boot.web.reactive.error.ErrorAttributes”类型的合格bean:

[英]NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available:

我正在关注https://www.baeldung.com/spring-webflux-errors教程来处理 Spring Webflux 项目中的错误。

package com.example.demo.exception;

import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

import java.util.Map;

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

    public ExceptionWebHandler(ErrorAttributes errorAttributes,
                               ApplicationContext applicationContext,
                               ServerCodecConfigurer serverCodecConfigurer) {
        super(errorAttributes, new ResourceProperties(), applicationContext);
        super.setMessageWriters(serverCodecConfigurer.getWriters());
        super.setMessageReaders(serverCodecConfigurer.getReaders());
    }
    @Override
    protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) {
        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
    }

    private Mono<ServerResponse> renderErrorResponse(ServerRequest serverRequest) {
        Map<String,Object> errorAttributes = getErrorAttributes(serverRequest, false);
        return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromObject(errorAttributes.get("message")));
    }
}

运行项目时出现以下错误。

通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available: 预计至少有 1 个 bean 有资格作为自动装配候选者。 依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

描述:

com.example.demo.exception.ExceptionWebHandler 中构造函数的参数 0 需要找不到类型为“org.springframework.boot.web.reactive.error.ErrorAttributes”的 bean。

找到以下候选但无法注入: - 未加载“ErrorWebFluxAutoConfiguration”中的 Bean 方法“errorAttributes”,因为不是反应式 web 应用程序

行动:

考虑重新访问上面的条目或在配置中定义类型为“org.springframework.boot.web.reactive.error.ErrorAttributes”的 bean。

我不确定应该自动装配哪个 object。 错误消息说是 ErrorAttributes,但我不知道怎么做。 我尝试将@Autowired 添加到构造函数中的参数中,并作为class 的属性单独添加,但没有帮助。

The following candidates were found but could not be injected: - Bean method 'errorAttributes' in 'ErrorWebFluxAutoConfiguration' not loaded because not a reactive web application

可能是您的问题或至少是其中一个问题。

Spring 文档在最后一段中指出:

在应用程序中同时添加spring-boot-starter-webspring-boot-starter-webflux模块会导致 Spring 引导自动配置 Spring MVC,而不是 WebFlux。 之所以选择此行为,是因为许多 Spring 开发人员将spring-boot-starter-webflux到他们的 Spring MVC 应用程序以使用响应式 WebClient。 您仍然可以通过将所选应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)来强制执行您的选择。

因此,在您的应用程序的某个地方,您可以直接或通过其他一些依赖项传递地引入spring-web

这反过来会导致您的应用程序将自动配置为非 webflux 应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM