RestTemplate: How to send URL and query parameters together

I am trying to pass path param and query params in a URL but I am getting a weird error. below is the code

    String url = "{id}/Identifier"
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "1234");
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
                                        .queryParam("name", "myName");
    String uriBuilder =; , HttpMethod.PUT, requestEntity,
                    class_p, params);

and my url is becoming

what should I do to make it work. I am expecting{id}/Identifier?name=myName so that params will add id to the url

please suggest. thanks in Advance


I would use buildAndExpand from UriComponentsBuilder to pass all types of URI parameters.

For example:

String url = "{planet}/moons/{moon}";

// URI (URL) parameters
Map<String, String> urlParams = new HashMap<>();
urlParams.put("planets", "Mars");
urlParams.put("moons", "Phobos");

// Query parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
        // Add query parameter
        .queryParam("firstName", "Mark")
        .queryParam("lastName", "Watney");

 * Console output:
 */ , HttpMethod.PUT,
        requestEntity, class_p);

 * Log entry:
 * org.springframework.web.client.RestTemplate Created PUT request for ""

Leave a Reply

Your email address will not be published. Required fields are marked *