Java NoSuchBeanDefinitionException:没有类型为';org.springframework.boot.web.reactive.error.ErrorAttributes';提供:
我将按照教程来处理SpringWebFlux项目中的错误Java NoSuchBeanDefinitionException:没有类型为';org.springframework.boot.web.reactive.error.ErrorAttributes';提供:,java,spring-boot,spring-webflux,Java,Spring Boot,Spring Webflux,我将按照教程来处理SpringWebFlux项目中的错误 package com.example.demo.exception; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; import org.spri
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")));
}
}
package com.example.demo.exception;
导入org.springframework.boot.autoconfigure.web.ResourceProperties;
导入org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
导入org.springframework.boot.web.reactive.error.ErrorAttributes;
导入org.springframework.context.ApplicationContext;
导入org.springframework.core.annotation.Order;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.codec.servercodeconfigurer;
导入org.springframework.stereotype.Component;
导入org.springframework.web.reactive.function.BodyInserters;
导入org.springframework.web.reactive.function.server.*;
导入reactor.core.publisher.Mono;
导入java.util.Map;
@组成部分
@订单(-2)
公共类ExceptionWebHandler扩展了AbstractErrorWebExceptionHandler{
公共例外WebHandler(ErrorAttributes ErrorAttributes,
应用上下文应用上下文,
服务器编解码器配置器(服务器编解码器配置器){
超级(errorAttributes,new ResourceProperties(),applicationContext);
super.setMessageWriters(ServerCodeConfigurer.getWriters());
super.setMessageReaders(ServerCodeConfigurer.getReaders());
}
@凌驾
受保护的路由函数getRoutingFunction(最终错误属性ErrorAttributes){
返回RouterFunctions.route(RequestPredicates.all(),this::renderErrorResponse);
}
专用Mono renderErrorResponse(服务器请求服务器请求){
Map errorAttributes=getErrorAttributes(serverRequest,false);
返回ServerResponse.status(HttpStatus.INTERNAL\u SERVER\u错误)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(errorAttributes.get(“message”));
}
}
我在运行项目时遇到以下错误
通过构造函数参数0表示的未满足依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.boot.web.reactive.error.ErrorAttributes”类型的合格bean可用:至少需要1个符合autowire候选条件的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添加到构造函数中的参数中,并单独作为类的属性添加,但没有帮助。
找到了以下候选项,但无法注入:“'ErrorWebFluxAutoConfiguration'中的Bean方法'errorAttributes'未加载,因为不是反应式web应用程序
可能是你的问题,或者至少是其中一个问题
Spring文档说明如下:
在应用程序中添加springbootstarterweb
和springbootstarterwebflux
模块会导致springboot自动配置springmvc,而不是webflux。之所以选择这种行为,是因为许多Spring开发人员将Spring boot starter webflux
添加到他们的Spring MVC应用程序中,以使用反应式WebClient。您仍然可以通过将所选应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.Responsive)来强制您的选择
因此,在应用程序中的某个地方,您可以直接或通过其他依赖项传递地拉入SpringWeb
这反过来会导致您的应用程序将自动配置为非webflux应用程序。
找到了以下候选程序,但无法注入:-未加载“ErrorWebFluxAutoConfiguration”中的Bean方法“errorAttributes”,因为不是反应性web应用程序。
可能是你的问题,或者至少是其中一个问题
Spring文档说明如下:
在应用程序中添加springbootstarterweb
和springbootstarterwebflux
模块会导致springboot自动配置springmvc,而不是webflux。之所以选择这种行为,是因为许多Spring开发人员将Spring boot starter webflux
添加到他们的Spring MVC应用程序中,以使用反应式WebClient。您仍然可以通过将所选应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.Responsive)来强制您的选择
因此,在应用程序中的某个地方,您可以直接或通过其他依赖项传递地拉入SpringWeb
这将导致您的应用程序自动配置为非webflux应用程序。谢谢。是的,我的依赖项中有spring boot starter web和spring boot starter webflux。在我删除spring-boot-starter-web之后,它就工作了。谢谢。是的,我的依赖项中有spring boot starter web和spring boot starter webflux。在我移除SpringBootStarterWeb之后,它就工作了。