How do I Parse JSON Responses from Java 11 HttpClient Easily?

To parse JSON responses easily using Java 11’s HttpClient, you can use libraries like Jackson or Gson that help in converting a JSON string to Java objects (POJOs) without hassle.

Here’s a step-by-step guide:

Step 1: Create a Java 11 HttpClient request

  1. Use Java’s HttpClient to make an HTTP request and get the JSON response as a string.
  2. Use HttpRequest or HttpResponse as part of the Java 11 HttpClient API.

Step 2: Add Jackson or Gson to Parse JSON

  1. Jackson: Add the dependency to your pom.xml (for Maven):
    <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency> 
  2. Gson: Add this dependency:
    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> 

Step 3: Example Using Jackson

package org.kodejava.net.http; import com.fasterxml.jackson.databind.ObjectMapper; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; public class HttpClientJacksonExample { public static void main(String[] args) throws Exception { // 1. Create an HttpClient HttpClient client = HttpClient.newHttpClient(); // 2. Create an HttpRequest HttpRequest request = HttpRequest.newBuilder() .uri(new URI("https://jsonplaceholder.typicode.com/posts/1")) // Example URL .GET() .build(); // 3. Send the HttpRequest and get an HttpResponse HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); // 4. Parse JSON String Response to a Java Object using Jackson ObjectMapper mapper = new ObjectMapper(); Post post = mapper.readValue(response.body(), Post.class); // 5. Use the parsed object System.out.println("Post Title: " + post.getTitle()); } // Sample POJO to match the JSON structure public static class Post { private int userId; private int id; private String title; private String body; // Getters and Setters public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } } } 

Step 4: Example Using Gson

package org.kodejava.net.http; import com.google.gson.Gson; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; public class HttpClientGsonExample { public static void main(String[] args) throws Exception { // 1. Create HttpClient HttpClient client = HttpClient.newHttpClient(); // 2. Create HttpRequest HttpRequest request = HttpRequest.newBuilder() .uri(new URI("https://jsonplaceholder.typicode.com/posts/1")) // Example URL .GET() .build(); // 3. Send HttpRequest and get HttpResponse HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); // 4. Parse JSON String response to Java Object using Gson Gson gson = new Gson(); Post post = gson.fromJson(response.body(), Post.class); // 5. Use the parsed object System.out.println("Post Title: " + post.getTitle()); } // Sample POJO class to match the JSON structure public static class Post { private int userId; private int id; private String title; private String body; // Getters and Setters public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } } } 

Key Points:

  • Serialization and Deserialization: POJO structure must match your JSON’s keys.
  • Why Jackson or Gson?
    They are robust and simplify working with JSON (and even converting nested structures).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.