Application Entry Point
Relevant source files
The following files were used as context for generating this wiki page:- complete/src/main/java/com/example/restservice/RestServiceApplication.java
- complete/src/main/java/com/example/restservice/GreetingController.java
- complete/src/main/java/com/example/restservice/Greeting.java
- complete/pom.xml
- README.adoc
- complete/src/test/java/com/example/restservice/GreetingControllerTests.java
The application entry point is defined in the RestServiceApplication class. This class is responsible for bootstrapping and launching the Spring Boot application, which creates a "Hello, World" RESTful web service. It leverages Spring Boot's auto-configuration to set up an embedded web server, scan for components like controllers, and configure the application to handle HTTP requests. The service exposes a /greeting endpoint that returns a JSON object.
The primary function of this entry point is to start the service, making it ready to accept GET requests at http://localhost:8080/greeting. The response is a JSON representation of a greeting, which can be customized with a name query parameter.
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java, README.adoc
Application Bootstrap
The application is launched using the standard Java main method within the RestServiceApplication class. This method calls SpringApplication.run(), which starts the entire Spring Framework application context.
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java:9-11
// complete/src/main/java/com/example/restservice/RestServiceApplication.java
@SpringBootApplication
public class RestServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RestServiceApplication.class, args);
}
}
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java:6-13
The diagram below illustrates the startup process initiated by the main method.
This diagram shows the sequence of events when the application starts.
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java:10, README.adoc
Core Components
The application is built around three main components: the application class, a REST controller, and a resource representation class (data model).
The Main Application Class
RestServiceApplication is the central class, annotated with @SpringBootApplication. This single annotation is a convenience that combines three other annotations:
@Configuration: Tags the class as a source of bean definitions for the application context.@EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.@ComponentScan: Tells Spring to look for other components, configurations, and services in thecom.example.restservicepackage.
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java:6, README.adoc
The REST Controller and Data Model
The GreetingController handles incoming HTTP requests, and the Greeting record models the data for the response.
This diagram shows the relationship between the main application class, the controller, and the data model.
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java, complete/src/main/java/com/example/restservice/GreetingController.java, complete/src/main/java/com/example/restservice/Greeting.java
GreetingController
This class is marked with @RestController, indicating that it's a controller where every method returns a domain object instead of a view. It handles GET requests for /greeting.
Sources: complete/src/main/java/com/example/restservice/GreetingController.java:9, README.adoc
Greeting Record
This is a Java record class that models the id and content of the greeting. Instances of this class are automatically converted to JSON by the Jackson 2 library, which is included by default with the spring-boot-starter-web dependency.
// complete/src/main/java/com/example/restservice/Greeting.java
package com.example.restservice;
public record Greeting(long id, String content) { }
Sources: complete/src/main/java/com/example/restservice/Greeting.java:3, README.adoc
Request Handling Flow
When a client sends a GET request to /greeting, the GreetingController's greeting method is invoked.
The following sequence diagram shows the flow of a typical request.
This diagram shows the sequence of a GET request to the /greeting endpoint.
Sources: complete/src/main/java/com/example/restservice/GreetingController.java:14-17, README.adoc
Endpoint Details
The /greeting endpoint is defined by the @GetMapping annotation.
| Property | Value | Description | Source File |
|---|---|---|---|
| Path | /greeting | The URL path for the endpoint. | GreetingController.java:14 |
| HTTP Method | GET | The HTTP method handled by the endpoint. | GreetingController.java:14 |
| Request Parameter | name | Optional. A string to customize the greeting. | GreetingController.java:15 |
| Default Value | World | The value used if the name parameter is absent. | GreetingController.java:15 |
| Response | JSON | A JSON object representing the Greeting. | README.adoc |
Dependencies
The application's functionality is enabled by dependencies defined in the pom.xml file. The core dependency is spring-boot-starter-web, which bundles everything needed for building web applications, including RESTful applications, using Spring MVC. It also includes an embedded Tomcat server by default.
| Group ID | Artifact ID | Scope | Description |
|---|---|---|---|
org.springframework.boot | spring-boot-starter-web | compile | Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container. |
org.springframework.boot | spring-boot-starter-test | test | Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito. |
Sources: complete/pom.xml:21-29
Testing
The application includes tests to verify the behavior of the GreetingController. The GreetingControllerTests class uses @SpringBootTest to load the full application context and @AutoConfigureMockMvc to configure a MockMvc instance for sending HTTP requests to the controller without needing a running server.
The tests verify two scenarios:
- A request to
/greetingwithout anameparameter returns the default message "Hello, World!". - A request to
/greetingwith anameparameter returns a tailored message.
// complete/src/test/java/com/example/restservice/GreetingControllerTests.java
@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:38-42