Consume Restful Web Service Java Example

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:

HttpClient

  • 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.
  • Example:
  • If HTTP/2 is not supported by the server, processes the request using HTTP/1.1
  • You can use executor() for asynchronous tasks.

HttpRequest

  • 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().
  • Example:

HttpRequest.BodyPublisher/HttpRequest.BodyPublishers

  • 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.

HttpResponse

  • Once after HttpRequest is sent by the client, HttpResponse is received which includes headers and a message body, if any.
  • Example:
  • sendAsync() is used to send the HttpRequest Asynchronously(non-blocking) and returns CompletableFuture.

HttpResponse.BodyHandler/HttpResponse.BodyHandlers

  • 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

  • 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.

Software Required:

  1. Eclipse
  2. JDK 11
  3. MySQL 8.0

Create RESTful web service application using Spring Boot 2.1.x with the following project structure.

productservice

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:

http://localhost:8080/emp/controller/getDetails

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.

Conclusion

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.

waldockwitter.blogspot.com

Source: https://dzone.com/articles/java-11-http-client-api-to-consume-restful-web-ser-1

0 Response to "Consume Restful Web Service Java Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel