adding request param to every request using spring resttemplate
Asked Answered
S

4

6

I am trying to add common request parameters to every request using RestTemplate. For example if my url is http://something.com/countries/US then I want to add common request param ?id=12345. This common request parameter needs to be added on all request. I don't want to add this on each call and want something common.

this post has answer that was marked correct, but I am not sure how you can add request parameters on org.springframework.http.HttpRequest

Any other way I can achieve this ?

Savina answered 28/8, 2019 at 16:51 Comment(0)
L
16

To add request parameters to the HttpRequest , you can first use UriComponentsBuilder to build an new URI based on the existing URI but adding the query parameters that you want to add.

Then use HttpRequestWrapper to wrap the existing request but only override its URI with the updated URI.

Code wise it looks like:


public class AddQueryParameterInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {

        URI uri = UriComponentsBuilder.fromHttpRequest(request)
                .queryParam("id", 12345)
                .build().toUri();
        
        HttpRequest modifiedRequest = new HttpRequestWrapper(request) {
        
            @Override
            public URI getURI() {
                return uri;
            }
        };
        return execution.execute(modifiedRequest, body);
    }

}

And add the interceptor to the RestTemplate:

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new AddQueryParamterInterceptor());

restTemplate.setInterceptors(interceptors);
Luxuriance answered 28/8, 2019 at 17:35 Comment(1)
UriComponentsBuilder.fromHttpRequest is deprecated now since Spring 6.1. use instead ForwardedHeaderUtils.adaptFromForwardedHeadersJehol
J
1

for Spring 6.1 / Spring Boot 3.2.x

    // inject apikey in URI query parameter
    ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {

        HttpRequest modifiedRequest = new HttpRequestWrapper(request) {

            @Override
            public URI getURI() {
                return ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())
                        .queryParam("apikey", omdbApikey)
                        .build(true)
                        .toUri();
            }
        };

        return execution.execute(modifiedRequest, body);
    };

The interceptor can be used in a RestTemplate/WebClient/RestClient

example:

        RestClient restClient = RestClient.Builder
                .requestInterceptor(interceptor)
                .build();
Jehol answered 3/3 at 12:3 Comment(0)
F
0

Two things are required to add common request parameters to every request using RestTemplate.

  1. Create a prototype bean RestTemplate
@Configuration
public class RestTemplateConfig {
    @Bean
    @Scope(
            value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
            proxyMode = ScopedProxyMode.TARGET_CLASS)
    public RestTemplate restTemplate() {
        RestTemplate localRestTemplate = new RestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = localRestTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new AddQueryParamterInterceptor());
        localRestTemplate.setInterceptors(interceptors);
        return localRestTemplate;
    }
}
  1. Use the Interceptor code as suggested by @ken-chan to add the request parameters. A new instance of Resttemaple will be created and for each and every request.
Fourteen answered 28/8, 2019 at 17:48 Comment(0)
S
-1

You can achieve this by adding interceptor to rest template

Suppurative answered 28/8, 2019 at 17:36 Comment(1)
that I know. but how was my question.Savina

© 2022 - 2024 — McMap. All rights reserved.