简体   繁体   English

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

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

I'm following https://www.baeldung.com/spring-webflux-errors tutorial to handle errors in Spring Webflux project.我正在关注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")));
    }
}

I'm getting the following error when running the project.运行项目时出现以下错误。

Unsatisfied dependency expressed through constructor parameter 0;通过构造函数参数 0 表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available: 预计至少有 1 个 bean 有资格作为自动装配候选者。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Description:描述:

Parameter 0 of constructor in com.example.demo.exception.ExceptionWebHandler required a bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' that could not be found. com.example.demo.exception.ExceptionWebHandler 中构造函数的参数 0 需要找不到类型为“org.springframework.boot.web.reactive.error.ErrorAttributes”的 bean。

The following candidates were found but could not be injected: - Bean method 'errorAttributes' in 'ErrorWebFluxAutoConfiguration' not loaded because not a reactive web application找到以下候选但无法注入: - 未加载“ErrorWebFluxAutoConfiguration”中的 Bean 方法“errorAttributes”,因为不是反应式 web 应用程序

Action:行动:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' in your configuration.考虑重新访问上面的条目或在配置中定义类型为“org.springframework.boot.web.reactive.error.ErrorAttributes”的 bean。

I'm not sure which object should I autowire.我不确定应该自动装配哪个 object。 The error message says its for ErrorAttributes, but I dont know how.错误消息说是 ErrorAttributes,但我不知道怎么做。 I tried adding @Autowired to the parameter in the constructor and separately as a property of the class, but it didn't help.我尝试将@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

is probably your problem or at least one of the problems.可能是您的问题或至少是其中一个问题。

The Spring documentation states in the last paragraph : Spring 文档在最后一段中指出:

Adding both spring-boot-starter-web and spring-boot-starter-webflux modules in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux.在应用程序中同时添加spring-boot-starter-webspring-boot-starter-webflux模块会导致 Spring 引导自动配置 Spring MVC,而不是 WebFlux。 This behavior has been chosen because many Spring developers add spring-boot-starter-webflux to their Spring MVC application to use the reactive WebClient.之所以选择此行为,是因为许多 Spring 开发人员将spring-boot-starter-webflux到他们的 Spring MVC 应用程序以使用响应式 WebClient。 You can still enforce your choice by setting the chosen application type to SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE) .您仍然可以通过将所选应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)来强制执行您的选择。

So somewhere in your application you are pulling in spring-web either directly or transitively through some other dependency.因此,在您的应用程序的某个地方,您可以直接或通过其他一些依赖项传递地引入spring-web

This in turn will result in that your application will automatically be configured as a non-webflux application.这反过来会导致您的应用程序将自动配置为非 webflux 应用程序。

暂无
暂无

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

相关问题 NoSuchBeanDefinitionException:没有可用的“org.springframework.security.config.annotation.web.builders.HttpSecurity”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为&#39;com.example.simple.RealApplication&#39;的合格bean - org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.simple.RealApplication' available 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合条件的 bean 类型 - Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type NoSuchBeanDefinitionException:没有“org.springframework.mail.MailSender”类型的合格bean - NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.MailSender' org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为[org.springframework.data.hadoop.hive.HiveOperations]的合格bean - org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.hadoop.hive.HiveOperations] 没有“org.springframework.boot.actuate.metrics.MetricsEndpoint”类型的合格bean - No qualifying bean of type 'org.springframework.boot.actuate.metrics.MetricsEndpoint' SpringBootTest:没有可用的“org.springframework.test.web.servlet.MockMvc”类型的合格bean: - SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: 错误没有可用的“org.springframework.security.oauth2.jwt.JwtDecoder”类型的合格bean - ERROR No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available Spring单元测试的结果在NoSuchBeanDefinitionException中:没有类型为&#39;org.springframework.mail.javamail.JavaMailSender&#39;的合格bean - Spring unit test results in NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' 我的@DataMongoTest 有问题 - 抛出 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type - Waht is wrong with my @DataMongoTest - throwing org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM