Java 11 HTTP Client API to Consume Restful Web Service Created Using Spring Boot
This article describes how to use Java 11 Http Client API to send HTTP GET/POST/PUT/DELETE requests.
Join the DZone community and get the full member experience.Join For Free
One of the features added in Java 11 is the standardized Http Client API. This article describes how to use Java 11 Http Client API to send HTTP GET/POST/PUT/DELETE requests.
This new API supports HTTP / 1.1 as well as HTTP 2. The newer version of the HTTP protocol is designed to improve the overall performance of sending requests by a client and receiving responses from the server. This is achieved by introducing several changes such as stream multiplexing, header compression and push promises.
In JDK 11, a new module is introduced as follows:
The package contains:
- Used to send requests and receive responses. Provides Synchronous and Asynchronous request mechanisms.
- HttpClient instance is immutable, once created you can send multiple requests with the same.
- To send requests, first you need to create HttpClient.
- If HTTP/2 is not supported by the server, processes the request using HTTP/1.1
- You can use executor() for asynchronous tasks.
- Create HttpRequest instance and set the URI, request method optionally specify the body and headers.
- HttpRequest instance is immutable and can be sent multiple times.
- Client supports all HTTP methods. Methods available to make different requests are GET(), POST(), PUT(), DELETE(), method().
- BodyPublisher is used to send the request with a request body.
- BodyPublishers are responsible for publishing the body content from a String or a File.
- In the above example, created BodyPublisher using BodyPublishers.ofString() and passing it as an argument to POST() method.
- Once after HttpRequest is sent by the client, HttpResponse is received which includes headers and a message body, if any.
- sendAsync() is used to send the HttpRequest Asynchronously(non-blocking) and returns CompletableFuture.
- HttpResponse.BodyHandler determines how to handle response body. The BodyHandler is invoked once the response status code and headers are available, but before the response body bytes are received. The BodyHandler is responsible for creating the BodySubscriber.
- HttpResponse.BodyHandlers provides factory methods for creating Body Handler. Accumulates the response bytes in memory until it is completely received, after which it is converted into the high-level java type like String.
- HttpResponse.BodySubscriber is a reactive-stream subscriber that receives streams of data with non-blocking back pressure.
Consider you have created a RESTful web service using Spring Boot. You can use Java 11 Http Client API to send the request asynchronously and to receive the response. No need to add any external library to your project.
- JDK 11
- MySQL 8.0
Create RESTful web service application using Spring Boot 2.1.x with the following project structure.
1. You can refer POM.xml here:
2. Mention the below in application.properties file.
3. Create ProductBean class in com.example.productservice.business.bean package.
4. Create ProductController class in com.example.productservice.controller package.
5. Create ProductService Inteface in com.example.productservice.service package.
6. Create ProductServiceImpl class in com.example.productservice.service package.
7. Create ProductDAO interface in com.example.productservice.dao package.
8. Create ProductDAOWrapper class in com.example.productservice.dao package.
9. Create ProductEntity Class in com.example.productservice.entity package.
Run the application using clean install spring-boot:run in maven goal.
Open Browser and hit the Url as:
Create a Java Application with JDK 11 to consume the ProductService application created:
1. Add Jackson-annotations-2.7.3, Jackson-core-2.7.3, Jackson-databind-2.7.3 jar files in the module path.
2. Make the HttpClientAsyncDemo module as open.
3. Create JSONUtils class in com.httpclientdemo.utility package. Used to convert JSON into Object and vice versa.
4. Create ProductBean class in com.httpclientdemo.business.bean package as same as ProductService application.
5. Create a main java class HttpClientAsyncDemo class in com.httpclientdemo.uiclient package.
Run the application. You can observe Product details added in the database table.
Uncomment the getProductDetailsById() in the main method, comment the other methods and run the application you can observe the product details displayed in the console.
Likewise, you can comment and uncomment the functionalities provided in the main class and observe the results.
With the HttpClient client in Java 11, we can send different types of requests Synchronously/Asynchronously with support of HTTP/1.1 and HTTP 2. No need to add any external http client library to your project.
Opinions expressed by DZone contributors are their own.