Skip to content

harman-04/spring-boot-json-client-server-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot: JSON Producer & Consumer Pattern

Project Overview

This project demonstrates the full lifecycle of a web request. It bridges the gap between Headless REST APIs and User-Facing Web Pages. The application acts as both a provider of data and a consumer, showing how Java objects are converted to JSON and back again.


Technical Concepts

1. Producing JSON (@RestController)

The RestJsonResponse class is the "Server" component.

  • produces = "application/json": Explicitly tells the client that the response will be formatted as JSON.
  • ResponseEntity: Allows us to control not just the body of the response, but also the HTTP Headers and Status Codes (e.g., returning 201 Created).

2. Consuming JSON (RestTemplate)

The ConsumeResponse class acts as the "Client".

  • getForEntity(): This method performs a GET request to a URL and automatically unmarshals (converts) the JSON response back into a Java DomainBean object.
  • Path Variables: Shows how to pass dynamic data into a URL template using placeholders like {id} and {name}.

3. Rendering Data (Thymeleaf)

While the API handles the data, Thymeleaf handles the presentation.

  • The ResponseController (a standard @Controller) takes the data from the API consumer and pushes it into a Model.
  • The Output.html template uses th:text attributes to display this data to the end-user.

Component Reference

DomainBean.java

A simple POJO (Plain Old Java Object) enhanced by Lombok's @Data. This object serves as the "Data Contract" shared between the producer and the consumer.

RestJsonResponse.java

Contains two types of endpoints:

  1. List Endpoint: Returns an ArrayList of users (demonstrates JSON arrays).
  2. Dynamic Endpoint: Takes path parameters and returns a custom ResponseEntity with a specialized status code.

Output.html

A Thymeleaf template that displays the properties of the consumed DomainBean and even renders the raw HTTP headers, giving you a look at the "metadata" of the network request.


Execution Flow

  1. The Call: You visit http://localhost:8080/ConsumeResponse/get in your browser.
  2. The Client: The ResponseController calls ConsumeResponse.get().
  3. The Network: RestTemplate makes an internal HTTP call to the /JSON/... endpoint.
  4. The Server: The RestJsonResponse generates a DomainBean, wraps it in a ResponseEntity, and sends it back as JSON.
  5. The Conversion: RestTemplate converts JSON back to a Java object.
  6. The UI: Thymeleaf renders the Java object properties into HTML.

How to Test

  1. Run Application: Start JsonRestResponseApplication.
  2. Test REST Producer: Open http://localhost:8080/JSON/data to see the raw JSON array of Geeks.
  3. Test Full Flow: Open http://localhost:8080/ConsumeResponse/get.
    • You should see the name @harsh and the ID 06 displayed in forest green.
    • You will also see the full HTTP Headers list printed below the data.

About

A Spring Boot 4.0.1 project showcasing REST API production, consumption via RestTemplate, and UI rendering with Thymeleaf.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors