Data Model (Greeting)
Relevant source files
The following files were used as context for generating this wiki page:- complete/src/main/java/com/example/restservice/Greeting.java
- complete/src/main/java/com/example/restservice/GreetingController.java
- README.adoc
- complete/src/test/java/com/example/restservice/GreetingControllerTests.java
- complete/pom.xml
- complete/src/main/java/com/example/restservice/RestServiceApplication.java
The Greeting data model is a resource representation class that serves as the data transfer object (DTO) for the RESTful web service. Its primary purpose is to structure the JSON response for the /greeting endpoint. This model encapsulates the unique identifier and the textual content of the greeting message returned by the service.
The service is built using Spring Boot, which handles the automatic conversion (marshalling) of Greeting Java objects into JSON format for the HTTP response. This is facilitated by the Jackson JSON library, included as a dependency via the spring-boot-starter-web package.
Sources: README.adoc:10-16, 59-65, complete/pom.xml:21-24
Greeting Class Definition
The Greeting data model is defined as a Java record. This is a concise way to create an immutable data carrier class. It automatically generates the constructor, equals(), hashCode(), and toString() methods, as well as accessor methods for its fields.
Sources: complete/src/main/java/com/example/restservice/Greeting.java:3, README.adoc:59-65
Class Diagram
The following diagram shows the structure of the Greeting record.
Fields
The Greeting model contains two fields that constitute the JSON response body.
| Field | Type | Description |
|---|---|---|
id | long | A unique identifier for the greeting. |
content | String | The textual representation of the greeting message. |
Sources: complete/src/main/java/com/example/restservice/Greeting.java:3, README.adoc:54-57
Instantiation and Usage
The Greeting object is created and returned by the greeting() method within the GreetingController.
Sources: complete/src/main/java/com/example/restservice/GreetingController.java:13-16
Controller Logic
The GreetingController is responsible for handling GET requests to the /greeting endpoint.
- It maintains a thread-safe
AtomicLongcounter to generate a uniqueidfor each request. - It uses a
templatestring ("Hello, %s!") to format thecontent. - The
contentis populated using anameparameter from the request's query string. If thenameparameter is not provided, it defaults to"World". - A new
Greetingobject is instantiated with the incremented counter value and the formatted content string.
Sources: complete/src/main/java/com/example/restservice/GreetingController.java:10-16, README.adoc:90-94
// complete/src/main/java/com/example/restservice/GreetingController.java
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
Sources: complete/src/main/java/com/example/restservice/GreetingController.java:14-16
Data Flow and Serialization
When a client sends a GET request to /greeting, the Spring DispatcherServlet routes it to the GreetingController. The controller's greeting method constructs a Greeting object, which is then returned. Because the controller is annotated with @RestController, Spring's MappingJackson2HttpMessageConverter automatically serializes this object into a JSON string and writes it directly to the HTTP response body.
Sources: README.adoc:72-74, 95-107
Request-Response Sequence
This sequence diagram illustrates the entire flow, from the client request to the JSON response.
Example JSON Output
The Greeting object is marshalled into a JSON structure like the following examples.
Default Greeting: (/greeting)
{
"id": 1,
"content": "Hello, World!"
}
Sources: README.adoc:12-15, complete/src/test/java/com/example/restservice/GreetingControllerTests.java:35-36
Customized Greeting: (/greeting?name=User)
{
"id": 2,
"content": "Hello, User!"
}
Sources: README.adoc:22-25, complete/src/test/java/com/example/restservice/GreetingControllerTests.java:41-43
Summary
The Greeting data model is a fundamental component of this REST service. Defined as an immutable Java record, it provides a simple and clear structure for the data returned by the /greeting endpoint. Its interaction with the GreetingController and Spring's automatic JSON serialization demonstrates a clean implementation of a RESTful API response model.
Sources: complete/src/main/java/com/example/restservice/Greeting.java, complete/src/main/java/com/example/restservice/GreetingController.java, README.adoc