Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a query parameter for all requests in Spring Webflux

How can I put query params into all requests in spring webflux avoiding code duplication in all places where I open http connection?

I try to make a configuration for a WebClient bean with a filter that should put a query param in URL every time when I do http call but I cannot find a way how to do it. Is it possible?

Configuration class:

@Configuration
public class IConnectionConfiguration {

    @Value("${api.key}")
    private String apiKey;

    @Value("${api.url}")
    private String url;

    @Bean
    public WebClient webClient(@Autowired ExchangeFilterFunction tokenFilter) {
        return WebClient.builder().baseUrl(url)
                .filters(exchangeFilterFunctions -> {
                    exchangeFilterFunctions.add(tokenFilter);
                })
                .build();
    }

    @Bean
    public ExchangeFilterFunction tokenFilter() {
        return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
            //todo put token into request as a query param
            return Mono.just(clientRequest);
        });
    }

}

Api calls layer:

@Service
public class Client {

    @Autowired
    private WebClient client;

    //todo test method
    public void testConnection() {
        String s = client.get()
                .uri(uriBuilder ->
                        uriBuilder.path("/something/{val}/something")
                              //todo line which I'm trying to avoid
                              //.queryParam("token", "token_value")
                                .build("123-test"))
                .retrieve()
                .bodyToMono(String.class)
                .block();

        System.out.println(s);
    }

}
like image 871
Aleksei Avatar asked Oct 28 '25 14:10

Aleksei


1 Answers

Another possibility that does not require overriding or using filter: Use UriComponentsBuilder with the query parameter that should be used for all requests. This instance of UriComponentsBuilder can than be used to instantiate a DefaultUriBuilderFactory for the WebClient.

Based on the code of the question:

public class IConnectionConfiguration {

    private static final String TOKEN_QUERY_PARAMETER = "TOKEN";

    @Value("${api.key}")
    private String apiKey;

    @Value("${api.url}")
    private String url;

    @Bean
    public WebClient webClient() {
        DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory(uriComponentsBuilder());
        return WebClient.builder()
                .uriBuilderFactory(defaultUriBuilderFactory)
                .build();
    }

    private UriComponentsBuilder uriComponentsBuilder() {
        return UriComponentsBuilder.fromHttpUrl(url)
                .queryParam(TOKEN_QUERY_PARAMETER, apiKey);
    }

}

The token will now be attached to every request that is issued by the WebClient.

like image 153
meberhard Avatar answered Oct 31 '25 06:10

meberhard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!