Hello everyone,

Greetings today!

Today we will look at how to call REST APIs using the Java 11 standardized HttpClient.

So let's get started.

To begin, we must create an instance of HttpClient and configure the connection timeout and version, which are the same regardless of the HTTP method, as shown below.

HttpClient httpClient = HttpClient.newBuilder()
    	 	.version(HttpClient.Version.HTTP_2)
              .connectTimeout(Duration.ofSeconds(30))
            .build();

Now we will see how we can call below API endpoints

  1. Get End Point: localhost:8080/book
  2. Get End Point: localhost:8080/book/1
  3. Get End Point: localhost:8080/book?id=1
  4. POST End Point with JSON Request Body
  5. PUT End Point with JSON Request Body
  6. DELETE End Point: localhost:8080/book/1
So let's start with 
  • Get End Point: localhost:8080/book
HttpRequest httpRequest 
	= HttpRequest.newBuilder()
     .header("Content-Type", "application/json")
     .GET()
     .uri(URI.create("http://localhost:8080/book/"))
      .build();

HttpResponse httpResponse
	= httpClient
    	.send(httpRequest, HttpResponse.BodyHandlers.ofString());

  • Get End Point: localhost:8080/book/1
For this, we just need to append a dynamic id at the end of the URI as shown below
HttpRequest httpRequest 
  = HttpRequest.newBuilder()
  .header("Content-Type", "application/json")
  .GET()
  .uri(
  URI.create("http://localhost:8080/book/"+bookDTO.getId()))
  .build();
 
HttpResponse httpResponse 
  = httpClient.send(httpRequest, 
  HttpResponse.BodyHandlers.ofString());
  • Get End Point: localhost:8080/book?id=1
Here the endpoint requires a query parameter that we can pass using UriComponentsBuilder used to build URI instance as shown below
URI uri = UriComponentsBuilder.newInstance()
		.scheme("http")
        .host("localhost")
        .port("8080")
        .path("book")
        .queryParam("id", "12")
        .build().toUri();
        
HttpRequest httpRequest1
	= HttpRequest.newBuilder()
    .header("Content-Type", "application/json")
    .GET()
    .uri(uri).build();
  • POST End Point with JSON Request Body
For converting the java instance to json I am using the GSON library and we can pass json payload in the POST method argument as shown below
 
HttpRequest httpRequest = HttpRequest.newBuilder()
    .header("Content-Type", "application/json")
    .POST(
      HttpRequest.BodyPublishers.ofString(gson.toJson(bookDTO))
     )
    .uri(URI.create("http://localhost:8080/book"))
    .build();

HttpResponse httpResponse
 = httpClient.send(
 	httpRequest,
 	HttpResponse.BodyHandlers.ofString()
  );
 
  • PUT End Point with JSON Request Body
For PUT as well we can pass payload as an argument as shown below.
HttpRequest httpRequest = HttpRequest.newBuilder()
  .header("Content-Type", "application/json")
  .PUT(HttpRequest.BodyPublishers.ofString(gson.toJson(bookDTO)))
  .uri(URI.create("http://localhost:8080/book"))
  .build();

HttpResponse httpResponse
 	= httpClient.send(
    httpRequest, 
    HttpResponse.BodyHandlers.ofString()
   );
  • DELETE End Point: localhost:8080/book/1
We can call the delete method in a similar manner we had called the GET endpoint
HttpRequest httpRequest = HttpRequest.newBuilder()
 .header("Content-Type", "application/json")
 .DELETE()
 .uri(
   URI.create("http://localhost:8080/book/"+bookDTO.getId()))
 .build();
  
HttpResponse httpResponse = 
  httpClient.send(
  httpRequest, HttpResponse.BodyHandlers.ofString()
 );

So we have covered how we can call any REST API using HttpClient, Let me know if you have any questions & if you like please share these articles with your colleagues.

Another post you may like to refer

 
Thank You 
Happy Learning!