Spring Framework
RestTemplate
GET method
API parameters
Java programming

Spring RestTemplate GET with query parameters

Codemia Knowledge Hub ·

To send a GET with query parameters through Spring's RestTemplate, either put {placeholders} in the URL and pass the values as a map, or build the URL with UriComponentsBuilder, which also encodes the values for you. Use RequestEntity with exchange() when you also need custom headers.

Query parameters are appended to the URI, for example http://api.example.com/users?profession=developer.

getForObject vs getForEntity

  • getForObject performs the GET and unmarshals the response body (if any) into the class you pass.
  • getForEntity does the same but returns a ResponseEntity, which includes the status details as well as the body.

Three ways to pass parameters

  1. URI variables: Placeholders in the URI are replaced by values from a map or from a list of arguments.
java
1    String uri = "http://api.example.com/users?profession={profession}";
2    Map<String, String> params = new HashMap<>();
3    params.put("profession", "developer");
4    User result = restTemplate.getForObject(uri, User.class, params);
  1. UriComponentsBuilder: More flexible and safer, because it handles URI syntax and encoding for you.
java
1    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://api.example.com/users")
2                                                        .queryParam("profession", "developer");
3    URI url = builder.build().toUri();
4    User result = restTemplate.getForObject(url, User.class);
  1. RequestEntity: GET requests normally have no body, but RequestEntity lets you add headers and other request details.
java
1    HttpHeaders headers = new HttpHeaders();
2    headers.set("Header-Key", "header-value");
3    URI uri = new URI("http://api.example.com/users?profession=developer");
4    RequestEntity<Void> requestEntity = RequestEntity.get(uri)
5                                                     .headers(headers)
6                                                     .build();
7    ResponseEntity<User> result = restTemplate.exchange(requestEntity, User.class);

Validation, errors and encoding

  • Validate parameters before sending them to avoid problems such as HTTP 400 responses.
  • Handle errors with try-catch, or catch specific exceptions like HttpClientErrorException and HttpServerErrorException.
  • Prefer UriComponentsBuilder for URLs with several query parameters, so special characters are encoded correctly.

Quick reference

MethodUsage Case
getForObject()Directly obtaining a POJO response.
getForEntity()When response headers or status codes are needed.
RestTemplate.exchange()Advanced configurations and full control over request.
ApproachDescription
URI VariablesSimple replacement from a map or list.
UriComponentsBuilderSafe building of complex URIs with automatic encoding.
RequestEntityAdding additional headers or initializations.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design