[英]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
andspring-boot-starter-webflux
modules in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux.在应用程序中同时添加
spring-boot-starter-web
和spring-boot-starter-webflux
模块会导致 Spring 引导自动配置 Spring MVC,而不是 WebFlux。 This behavior has been chosen because many Spring developers addspring-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.