In this article, you will learn how to make different kinds of HTTP GET requests by using the RestTemplate class in a Spring Boot application.

Simple GET Request

To make a GET HTTP request, you can use either getForObject() or getForEntity() method. Here is an example that uses the getForObject() method to fetch the user information as a JSON string:

// request url
String url = "https://jsonplaceholder.typicode.com/posts/1";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String json = restTemplate.getForObject(url, String.class);

// print json
System.out.println(json);

GET Request with Request Parameters

If you want to pass query parameters, you can either append them directly to URL or use placeholders. Here is an example of GET request made with query parameters appended to the URL:

// request url
String url = "https://google.com/search?q=java";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String html = restTemplate.getForObject(url, String.class);

Similarly, you can add placeholders to the URL for query parameters:

// request url
String url = "https://google.com/search?q={q}";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String html = restTemplate.getForObject(url, String.class, "java");

GET Request with Parameters and Headers

To add custom request headers to HTTP GET request, you should use the generic exchange() method provided by the RestTemplate class.

The following GET request is made with query parameters and request headers:

// request url
String url = "https://jsonplaceholder.typicode.com/posts/{id}";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();

// set `Content-Type` and `Accept` headers
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

// example of custom header
headers.set("X-Request-Source", "Desktop");

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<String> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        String.class,
        1
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}

GET Request with Basic Authentication

The following example demonstrates how to add basic authentication to RestTemplate GET request:

// request url
String url = "https://jsonplaceholder.typicode.com/posts";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();

// add basic authentication header
headers.setBasicAuth("username", "password");

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<String> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        String.class
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}

GET Request with Response Mapped to Java Object

Using RestTemplate, you can also map the JSON response directly to a Java object. Let us first create a simple model class:

Post.java

public class Post implements Serializable {

    private int userId;
    private int id;
    private String title;
    private String body;

    public Post() {
    }

    public Post(int userId, int id, String title, String body) {
        this.userId = userId;
        this.id = id;
        this.title = title;
        this.body = body;
    }

    // getters and setters, equals(), toString() .... (omitted for brevity)
}

Now we can simply use the Post class as response type in the exchange() method:

// request url
String url = "https://jsonplaceholder.typicode.com/posts/1";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<Post> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        Post.class
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}

Check out the Making HTTP Requests using RestTemplate in Spring Boot guide for more RestTemplate examples.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.