How to pass URL and query parameters in Spring REST client?

Let’s say you are making a GET request to a third party API from your spring application.

You want to pass both URL and query parameters.

First, what are URL and query parameters?

URL parameters:

URL parameters are passed along with the URL like this:

https://thirdpartyapi.com/request/1

In the above request , let’s assume the number 1 is dynamic. It represents an URL parameter.

You can have multiple URL parameters like this:

https://thirdpartyapi.com/request/1/requestId/101.

Here there are two URL parameters : request and requestId.

Query parameters:

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value.

Multiple parameters are separated by “&” symbol.

For example:

https://thirdpartyapi.com?request=1&requestId=101

The same parameters passed as URL parameters in the previous example are passed as Query parameters here.

How do we pass these parameters while using a Spring REST client ?

You can do that using exchange() method provided by Spring REST Template without using any additional classes.

For example, let’s say the below service is deployed at localhost:8080 and you want to consume it using Spring.

It is a simple GET service which takes in a single URL parameter (after the path /restService) and a single query parameter (supplied to the variable “queryParameter”)

The rest service just returns a json with the query parameter and the url parameter supplied in the GET request.

To invoke the above REST service using Spring template , follow the below steps:

STEP1: Construct a URL with the URL and query parameters enclosed in braces

STEP2: Use specific exchange() method of Spring REST template and supply the url and query parameters as a map input.

Here is it explained in detail:

STEP1: Construct URL with URL and Query parameters in braces:

To invoke the above URL , construct a URL with both the parameters in braces as below:

String url = "http://localhost:8080/restService/{urlParameter}?queryParameter= {queryParameter}";

Once constructed populate the url and query parameters in a map with the keys of the map matching the parameter names in braces:

Map<String, String> uriVariables = new HashMap<>();

uriVariables.put("urlParameter", "myURLParameter");
uriVariables.put("queryParameter", "myQueryParameter");

STEP2: Invoke specific exchange() method of Spring REST template:

Spring REST template has several overloaded exchange() methods.

Use the below one which allows you to pass both URL and query parameters in a single map:

	public <T> ResponseEntity<T> exchange(String url, HttpMethod method,
			@Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables)
			throws RestClientException

Here is the entire code:

package com.spring.restclient;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SampleRestClient {

	@GetMapping("/test")
	public Map<String, String> test() {
		
		String url = "http://localhost:8080/restService/{urlParameter}?queryParameter= {queryParameter}";

		RestTemplate template = new RestTemplate();

		HttpHeaders headers = new HttpHeaders();
		HttpEntity requestEntity = new HttpEntity<>(headers);

		Map<String, String> uriVariables = new HashMap<>();

		uriVariables.put("urlParameter", "myURLParameter");
		uriVariables.put("queryParameter", "myQueryParameter");

		ResponseEntity<Map> response = template.exchange(url, HttpMethod.GET, requestEntity, Map.class, uriVariables);

		
		return response.getBody();
	}
}

I just created another REST GET service named “test” and deployed it in another port (8081) and used Spring REST template to invoke the GET service deployed in 8080.

Here is the response:

That’s it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s