Search code examples
javaspringspring-bootresttemplate

Spring boot server receiving param with %20 instead of space


I have spring boot server with controller and one endpoint:

@GetMapping(path = "/readProperty", produces = "application/json")
public ResponseEntity<ReferenceDataRestResponse> getReferenceDatav4
        (WebRequest request) {
    Map<String, String[]> parametersMap = new HashMap<>(request.getParameterMap());

    ....

}

And another spring boot application which is sending request with restTemplate.getForEntity

String url = UriComponentsBuilder
   .fromHttpUrl(this.referenceDataBaseUrl)
   .path(this.referenceDataReadProperty)
   .queryParams(new LinkedMultiValueMap(multiValueParams))
   .toUriString();
log.info("Reference data URL {}", url);
ResponseEntity<String> responseEntity = this.refDataRestTemplate.getForEntity(url, String.class);

info log with url prints this:

Reference data URL http://some-name:8080/readProperty?channel=E&service_name=Some%20Value

Problem is that when I call this endpoint with curl command and with restTemplate.getForEntity I receive different values for service_name param in controller

1. Curl

When I copy this url from log and call it with curl command like this:

curl 'http://some-name:8080/readProperty?channel=E&service_name=Some%20Value'

My endpoint is receiving service_name param as

Some Value

2. Java call with restTemplate.getForEntity(...)

When java is calling this url with:

ResponseEntity<String> responseEntity = this.refDataRestTemplate.getForEntity(url, String.class, new Object[0]);

My endpoint is receiving service_name param as

Some%20Value

3. Question

How should I prepare request using spring boot to receive in controller decoded param?


Solution

  • Found an issue.

    Url is encoded twice by java service. Once by toUriString(), second time by restTemplate.getForEntity

    when I changed method which is sending request to:

    String url = UriComponentsBuilder
       .fromHttpUrl(this.referenceDataBaseUrl)
       .path(this.referenceDataReadProperty)
       .queryParams(new LinkedMultiValueMap(multiValueParams))
       .toString();
    log.info("Reference data URL {}", url);
    ResponseEntity<String> responseEntity = this.refDataRestTemplate.getForEntity(url, String.class);
    

    Then it worked, parameter value have spaces instead of %20