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.
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., returning201 Created).
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 JavaDomainBeanobject.- Path Variables: Shows how to pass dynamic data into a URL template using placeholders like
{id}and{name}.
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 aModel. - The
Output.htmltemplate usesth:textattributes to display this data to the end-user.
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.
Contains two types of endpoints:
- List Endpoint: Returns an
ArrayListof users (demonstrates JSON arrays). - Dynamic Endpoint: Takes path parameters and returns a custom
ResponseEntitywith a specialized status code.
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.
- The Call: You visit
http://localhost:8080/ConsumeResponse/getin your browser. - The Client: The
ResponseControllercallsConsumeResponse.get(). - The Network:
RestTemplatemakes an internal HTTP call to the/JSON/...endpoint. - The Server: The
RestJsonResponsegenerates aDomainBean, wraps it in aResponseEntity, and sends it back as JSON. - The Conversion:
RestTemplateconverts JSON back to a Java object. - The UI: Thymeleaf renders the Java object properties into HTML.
- Run Application: Start
JsonRestResponseApplication. - Test REST Producer: Open
http://localhost:8080/JSON/datato see the raw JSON array of Geeks. - 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.