Spring MVC can not decode query parameter value from restTemplate

Viewed 65

I have an Spring boot application. For one endpoint,

I use a swagger to call it. What I input is: "FGluxxdw==", then swagger will auto encode as "FGluxxdw%3D%3D", but the endpoint would auto decode the value back as "FGluxxdw==", following is the whole swagger request sample:

curl -X GET "http://localhost:8080/order/scroll?size=1&orderItemNumber=XX1468&scrollId=FGluxxdw%3D%3D" 
-H "accept: application/hal+json" 
-H "Authorization: Bearer SOME_VALUE" 
-H "api-key: ABC-123"

However, if I use restemplate,

this.restTemplate.exchange(uri, GET, httpEntity, PRODUCT.class))

And the uri value is

http://localhost:8080/order/scroll?size=1&orderItemNumber=XX1468&scrollId=FGluxxdw%3D%3D

However, spring boot endpoint can not decode the scrollId this time.

1 Answers

This question was asked a couple of months ago. Leaving this here to help others. Try:

  1. Use UriComponentsBuilder.

  2. Set charset to UTF_8.

  3. Build Uri not UriString.

    UriComponentsBuilder uriBuilder = UriComponentsBuilder
            .fromHttpUrl("http://localhost:8080/order/scroll")
            .encode(StandardCharsets.UTF_8)
            .queryParam("size", 1)
            .queryParam("orderItemNumber", "XX1468")
            .queryParam("scrollId", "FGluxxdw==");
    URI uri = uriBuilder.build().toUri();
    
Related