SoFunction
Updated on 2025-03-02

Solve the problem of SpringCloud Gateway's failure to use OpenFeign remote call

1. Framework version

Spring Boot、 Spring Cloud 、 Spring Cloud Alibaba

Frame Name Framework version
Spring Boot 2.2.
Spring Cloud Hoxton.SR3
Spring Cloud Alibaba 2.2.

2. Source code display

Remote authenticationControllerController code snippet

@RestController
@Slf4j
@RequestMapping("/security")
public class AuthenticationController {

    @Resource
    private AuthenticationService authenticationService;

    @PostMapping("/auth")
    public Boolean checkAuth(@RequestBody @Valid BaseRequest baseRequest) {
        ("<-------------------------->");
        return (baseRequest);
    }
}

Gateway referenceOpenFeignrely

<!-- OpenFeign -->
<dependency>
    <groupId></groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.</version>
</dependency>

GatewayOpenFeignCalling the client

@Component
@FeignClient(value = "rms-security-service")
public interface InvokeSecurityServiceClient {

    @PostMapping("/security/auth")
    Boolean checkAuth(BaseRequest baseRequest);
}

III. Problem description

In the process of unified authentication and authentication at the gateway level, the interface of the authentication server needs to be remotely called for authentication operations.

But in adoptionOpenFiegnDuring the remote call process, the following error report stack appears, resulting in the remote call failure.

  • Stack Information

: No qualifying bean of type '' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@(required=true)}
    at $(:384)
    Suppressed: $OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ $$Lambda$772/0x0000016c006c3040 [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ $ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/open-platform/achievement/list?page=1" [ExceptionHandlingWebHandler]
Stack trace:

  • Key information

No qualifying bean of type '

4. Solution

Stack Analysis

  • HttpMessageConvertersNot injected into the container to manage
  • ISSUEStrack
  • Click hereCheckissuesinformation

Solution

Source code:No exception was found in theAutoConfigurationOpen

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass()
@Conditional()
@AutoConfigureAfter({ , ,  })
@Import({ , ,
		 })
public class HttpMessageConvertersAutoConfiguration {
}

Source code:@Conditional() @ConditionalyesSpring4The newly provided annotation is to make judgments according to certain conditions and register the container with the conditions.beanCheck the class againNotReactiveWebApplicationCondition

static class NotReactiveWebApplicationCondition extends NoneNestedConditions {

		NotReactiveWebApplicationCondition() {
			super(ConfigurationPhase.PARSE_CONFIGURATION);
		}

		@ConditionalOnWebApplication(type = )
		private static class ReactiveWebApplication {

		}

	}

Spring Cloud GatewayIt is based onWebFluxYesReactiveWeb,soHttpMessageConvertersNot automatically injected.

So I copied the source code directly in the configuration fileBean, and finally succeed.

# Create a new configuration class@Configuration
public class WebFluxWithOpenFeignConfig {
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
        return new HttpMessageConverters(().collect(()));
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.