Search code examples
springspring-cloud-gateway

Multiple ProxyExchangeArgumentResolver Beans


I am working with spring-cloud-gateway and have the need to use multiple custom WebClients to proxy different requests.

Having one custom WebClient is no problem, see https://stackoverflow.com/a/64865408.

But if I define multiple beans of type ProxyExchangeArgumentResolver (each with a different supportsParameter implementation) the ProxyResponseAutoConfiguration fails at:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    argumentResolvers.add(context.getBean(ProxyExchangeArgumentResolver.class));
}

Because it is expecting only ONE bean of that type.

Any idea how to resolve this? Thanks in advance.


Solution

  • Ok I solved it by still having just one Bean of type ProxyExchangeArgumentResolver and handle the different WebClients within the resolveArgument Method:

    public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) {
        var webClientBeanName = parameter.getMethod().getAnnotation(UseWebClient.class).webClient();
        WebClient webClient = (WebClient) this.context.getBean(webClientBeanName);
    
        ProxyExchange<?> proxy = new ProxyExchange(webClient, exchange, bindingContext, this.type(parameter));
    ...
    }
    

    So I use a new Annotation to tell which WebClient to use. I however had to duplicate several things from the ProxyExchangeArgumentResolver.