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

Nir*_*mal 4 java spring-boot spring-webflux

我正在按照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")));
    }
}

Run Code Online (Sandbox Code Playgroud)

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

通过构造函数参数0表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“org.springframework.boot.web.reactive.error.ErrorAttributes”的合格 bean 可用:预计至少有 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。

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

Tho*_*olf 7

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 Boot 自动配置 Spring MVC,而不是 WebFlux。选择这种行为是因为许多 Spring 开发人员spring-boot-starter-webflux在他们的 Spring MVC 应用程序中添加了使用反应式 WebClient 的功能。您仍然可以通过将所选应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)来强制执行您的选择。

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

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