No qualifying bean of type ‘org.springframework.boot.web.reactive.error.ErrorAttributes’ available:

huangapple go评论67阅读模式
英文:

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.

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.fromValue(errorAttributes.get("message")));
    }
}

I'm getting the following error when running the project.

Unsatisfied dependency expressed through constructor parameter 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. Dependency annotations: {@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.

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

Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' in your configuration.

I'm not sure which object should I autowire. The error message says it's for ErrorAttributes, but I don't know how. I tried adding @Autowired to the parameter in the constructor and separately as a property of the class, but it didn't help.

英文:

I'm following https://www.baeldung.com/spring-webflux-errors tutorial to handle errors in Spring Webflux project.

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&lt;ServerResponse&gt; getRoutingFunction(final ErrorAttributes errorAttributes) {
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
}
private Mono&lt;ServerResponse&gt; renderErrorResponse(ServerRequest serverRequest) {
Map&lt;String,Object&gt; errorAttributes = getErrorAttributes(serverRequest, false);
return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(errorAttributes.get(&quot;message&quot;)));
}
}

I'm getting the following error when running the project.

Unsatisfied dependency expressed through constructor parameter 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. Dependency annotations: {@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.

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

Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' in your configuration.

I'm not sure which object should I autowire. The error message says its for ErrorAttributes, but I dont know how. I tried adding @Autowired to the parameter in the constructor and separately as a property of the class, but it didn't help.

答案1

得分: 6

以下是您要翻译的内容:

"以下找到了候选项,但无法注入:- 'ErrorWebFluxAutoConfiguration'中的Bean方法'errorAttributes'未加载,因为不是响应式Web应用程序"

"这可能是您的问题,或者至少是问题之一。"

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

在应用程序中同时添加'spring-boot-starter-web'和'spring-boot-starter-webflux'模块将导致Spring Boot自动配置Spring MVC,而不是WebFlux。选择此行为是因为许多Spring开发人员将'spring-boot-starter-webflux'添加到Spring MVC应用程序中以使用响应式Web客户端。您仍然可以通过将所选应用程序类型设置为_SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)_来强制执行您的选择。

因此,在您的应用程序的某个地方,您要么直接引入'spring-web',要么通过其他依赖项传递引入它。

这将导致您的应用程序自动配置为非WebFlux应用程序。

英文:

The following candidates were found but could not be injected: - Bean method &#39;errorAttributes&#39; in &#39;ErrorWebFluxAutoConfiguration&#39; 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:

> 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. This behavior has been chosen because many Spring developers add spring-boot-starter-webflux to their Spring MVC application to use the reactive WebClient. You can still enforce your choice by setting the chosen application type to SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE).

So somewhere in your application you are pulling in spring-web either directly or transitively through some other dependency.

This in turn will result in that your application will automatically be configured as a non-webflux application.

huangapple
  • 本文由 发表于 2020年7月31日 01:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63178617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定