RestTemplate: How to send URL and query parameters together

I would use buildAndExpand from UriComponentsBuilder to pass all types of URI parameters. For example: String url = “http://test.com/solarSystem/planets/{planet}/moons/{moon}”; // URI (URL) parameters Map<String, String> urlParams = new HashMap<>(); urlParams.put(“planet”, “Mars”); urlParams.put(“moon”, “Phobos”); // Query parameters UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url) // Add query parameter .queryParam(“firstName”, “Mark”) .queryParam(“lastName”, “Watney”); System.out.println(builder.buildAndExpand(urlParams).toUri()); /** * Console output: * http://test.com/solarSystem/planets/Mars/moons/Phobos?firstName=Mark&lastName=Watney … Read more