Java 11 HttpClient & Gson Tutorial: Send HTTP GET Requests and Parse JSON in a Single Source File Example

Java 11 HttpClient & Gson Tutorial: Send HTTP GET Requests and Parse JSON in a Single Source File Example

In this post we will give you information about Java 11 HttpClient & Gson Tutorial: Send HTTP GET Requests and Parse JSON in a Single Source File Example. Hear we will give you detail about Java 11 HttpClient & Gson Tutorial: Send HTTP GET Requests and Parse JSON in a Single Source File ExampleAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we’ll learn about the HttpClient library introduced in Java 11 for sending HTTP requests. We’ll also see how to use the Gson library from Google to parse JSON data.

We’ll be using a single source file for our app which can be executed in Java 11 using the java command without first compiling it (using javac)just like a script file.

Sending Http Requests Prior to Java 11

Prior to Java 11, developers had to use URLConnection to send HTTP requests. This package doesn’t have an easy to use API and doesn’t support the newer HTTP/2 protocol – As a result developers resorted to use third-party libraries like Apache HTTP Client

Note: HttpClient doesn’t currently have some advanced features like multi-part form data and compression support.


You will need to have Java 11 LTS version installed on your system. You can simply go to the official website and download the appropriate installer for your operating system. If you are using Ubuntu 19.04, you can also follow this tutorial to install Java 11 on your system.

You will also need some working knowledge of Java.

How to Use HttpClient in Java 11?

HttpClient is available from the package.

Using HttpClient is as easy as adding one line of code:


Or also:


You can also customize every aspect of the client:


These methods are used to change the default values of settings like:

  • HTTP/2,
  • Connection timeout,
  • Redirection policy,
  • Cookie handlers,
  • Authentication,
  • The proxy selector,
  • SSL.

You can also use HttpRequest available from the package to create requests and HttpResponse available from the package to work with response objects. For example:


Here, we specify the request URI using the uri() method , we call the GET() method to specify a GET request and and we call build() to create an instance of HttpRequest. You can also use the other methods such as GET(), POST(), DELETE() and PUT() or you can use method() to specify any HTTP method:


You can use BodyPublishers.noBody() for requests that don’t expect a body.

After building your request, you can send it using the send() method of the client:


BodyHandlers.ofString() converts the response raw data to a String. These are other options:


Note: You can also send requests asynchronously using the sendAsync() method. Check the docs for more information.

Java 11 & HttpClient Example

Now, let’s build a simple Java 11 example application that makes use of HttpClient to fetch data from a third-party REST API and display it. We’ll be using a news REST API available from newsapi.

You first need to head to their website and register for an API key.

After submitting the form. You will be redirected to a page where you can get your API key.

Java 11 Single Source Files or Scripts

Starting with Java 11, you can write script files as you don’t have to first compile your source code with javac before executing it with java.

For a single source code file, you can run the file directly with the java command and JVM will execute it. You can even use the shebang syntax in Unix based systems like Linux and macOS.

Open a new terminal, navigate inside a working folder and create a single file with the .java extension:

$ touch

Next, open the file and add the following code:

publicclassNewsScript{publicstaticvoidmain(String[]args){System.out.println("### NewsScript! v1.0: Get Daily News ###");}}

We simply create a Java class with a static main method and we call the println() method to display the ### NewsScript! v1.0: Get Daily News ### string on the console.

Let’s now run the file. Head back to your terminal and run the following command:

$ java

If you have Java 11, that should run the file and display the message on the console.

Sending HTTP GET Requests with Java 11+ HttpClient

Now, let’s actually get the news. In your source file add the following code:;;;;publicclassNewsScript{publicstaticvoidmain(String[]args){System.out.println("### NewsScript! v1.0: Get Daily News ###");StringAPI_KEY="<YOUR_API_KEY_HERE>";varclient=HttpClient.newHttpClient();varhttpRequest=HttpRequest.newBuilder().uri(URI.create(String.format("",API_KEY))).GET().build();try{varresponse=client.send(httpRequest,HttpResponse.BodyHandlers.ofString());System.out.println(response.body());}catch(Exceptione){e.printStackTrace();}}}

Make sure your replace <YOUR_API_KEY_HERE> with your actual API key from the News API.

If your run this code, you should get the news data printed as a JSON string with news entries.

Parsing JSON Data with Gson in Java 11

We need to parse the JSON string as a Java object so we can work with it.

Since Java doesn’t have any native JSON module, we’ll make use of gson, a library by Google for working with JSON in Java.

Since we are creating a single file script without Maven or Gradle, we need to download the jar file of the library and put it in the folder of our Java file.

Head to this Maven repo and download the .jar file.

Now, when running the script, you should add the current folder in the class path of your app as follows:

$ java --class-path './*'

Next, let’s parse our news data into Java objects. First, we need to create the following Data Transfer Objects or DTOs that correspond to the JSON structure of the returned data:


You need to import ArrayList using import java.util.ArrayList; in your code.

Note: These DTOs should be placed below the main class.

Next, import the Gson class:;

Next, change the code in the try{} catch(){} block inside the main() method as follows:

try{varresponse=client.send(httpRequest,HttpResponse.BodyHandlers.ofString());NewsDTOobj=newGson().fromJson(response.body(),NewsDTO.class);for(ArticleDTOart:obj.articles){System.out.println("### "+art.title+" ### n");System.out.println(art.description);System.out.println("nRead more: "+art.url+"n");}}catch(Exceptione){e.printStackTrace();}

We simply call the fromJson() method available from the Gson instance and we pass in the response body which contains a JSON string. This will convert the string to an object of the NewsDTO type.

Finally, we iterate over the articles array (of ArticleDTO objects) from the NewsDTO object and we print the title, description and URL of each item to the standard output.

That’s it! If we run our script using the following command:

$ java --class-path './*'

We should get something like the following screenshot:

Java 11 HttpClient Example

Enjoy your daily news from your terminal. You can click on each item’s URL to read the full article on your web browser.

This is the full code source of the Java 11 script:

importjava.util.ArrayList;;;;;;publicclassNewsScript{publicstaticvoidmain(String[]args){System.out.println("### NewsScript! v1.0: Get Daily News ###");StringAPI_KEY="<YOUR_API_KEY_HERE>";varclient=HttpClient.newHttpClient();varhttpRequest=HttpRequest.newBuilder().uri(URI.create(String.format("",API_KEY))).GET().build();try{varresponse=client.send(httpRequest,HttpResponse.BodyHandlers.ofString());//System.out.println(response.body());NewsDTOobj=newGson().fromJson(response.body(),NewsDTO.class);//System.out.println(obj.articles);for(ArticleDTOart:obj.articles){System.out.println("### "+art.title+" ### n");System.out.println(art.description);System.out.println("nRead more: "+art.url+"n");}}catch(Exceptione){e.printStackTrace();}}}classNewsDTO{Stringstatus;inttotalResults;ArrayList<ArticleDTO>articles;}classArticleDTO{SourceDTOsource;Stringauthor;Stringtitle;Stringdescription;Stringurl;}classSourceDTO{Stringid;Stringname;}


In this tutorial, we’ve used Java 11 HttpClient for sending HTTP requests with the Gson library for parsing JSON data to build a simple news application that allows you to read news from your terminal.

Hope this code and post will helped you for implement Java 11 HttpClient & Gson Tutorial: Send HTTP GET Requests and Parse JSON in a Single Source File Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US