Application Configuration
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the configuration of the gs-rest-service project. The application is built using Spring Boot, which favors convention over configuration, resulting in a setup that is both minimal and powerful. The configuration is managed through a combination of Maven build scripts, Spring Boot's auto-configuration capabilities triggered by annotations, and specific deployment manifests.
The primary configuration is handled by the Maven pom.xml file, which defines project dependencies and build settings. The application's behavior, particularly its web-facing components, is configured via annotations in the Java source code. For deployment, a manifest.yml file specifies environment details such as memory allocation and host information.
Maven Project Configuration
The core of the application's build and dependency management is configured in the complete/pom.xml file. It uses the Spring Boot starter parent to manage dependency versions and provides default plugin configurations.
Sources: complete/pom.xml
Parent POM
The project inherits from the spring-boot-starter-parent, which simplifies dependency management by providing sensible defaults for dependencies and plugins.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Sources: complete/pom.xml:5-10
Properties
The project is configured to use Java version 17.
<properties>
<java.version>17</java.version>
</properties>
Sources: complete/pom.xml:16-18
Dependencies
The application relies on a minimal set of dependencies, primarily for web and testing capabilities. The spring-boot-starter-web dependency includes everything needed to build a web application, including an embedded Tomcat server and the Jackson library for JSON conversion.
The following diagram illustrates the key dependencies defined in the POM.
Sources: complete/pom.xml:19-30
| Group ID | Artifact ID | Scope | Description |
|---|---|---|---|
org.springframework.boot | spring-boot-starter-web | compile | Provides dependencies for building RESTful web services using Spring MVC. |
org.springframework.boot | spring-boot-starter-test | test | Provides dependencies for testing Spring Boot applications. |
Sources: complete/pom.xml:19-30
Build Plugin
The spring-boot-maven-plugin is used to package the application as an executable JAR file, which includes all necessary dependencies and an embedded web server.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Sources: complete/pom.xml:34-37
Spring Boot Application Configuration
The application is bootstrapped via the RestServiceApplication class. Configuration at this level is done through a single, powerful annotation.
Application Entry Point
The @SpringBootApplication annotation is a convenience annotation that adds all of the following:
@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.
package com.example.restservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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
Web Endpoint Configuration
The web layer is configured in the GreetingController class, which defines the REST API endpoints.
Sources: complete/src/main/java/com/example/restservice/GreetingController.java
Controller and Mappings
The @RestController annotation marks the class as a controller where every method returns a domain object instead of a view. It is a shorthand for @Controller and @ResponseBody.
The @GetMapping("/greeting") annotation maps HTTP GET requests for the /greeting path to the greeting() method.
The following sequence diagram shows the flow of a request to the /greeting endpoint.
Request Parameters
The greeting method uses @RequestParam to bind the value of the name query string parameter to the name method parameter. If the parameter is not present, it uses a configured default value.
| Parameter | Annotation | Type | Default Value | Description |
|---|---|---|---|---|
name | @RequestParam | String | "World" | An optional name to include in the greeting message. |
@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
Deployment Configuration
The manifest.yml file provides configuration for deploying the application, likely to a platform like Cloud Foundry.
---
applications:
- name: gs-rest-service
memory: 256M
instances: 1
host: rest-service
domain: guides.spring.io
path: build/libs/gs-rest-service-0.1.0.jar
Sources: complete/manifest.yml
The configuration specifies the following properties:
| Property | Value | Description |
|---|---|---|
name | gs-rest-service | The name of the application. |
memory | 256M | The memory limit for each application instance. |
instances | 1 | The number of application instances to run. |
host | rest-service | The hostname for the application's route. |
domain | guides.spring.io | The domain for the application's route. |
path | build/libs/gs-rest-service-0.1.0.jar | The path to the application artifact to be deployed. |
Sources: complete/manifest.yml:2-8
Testing Configuration
Tests for the GreetingController are configured using annotations that set up a Spring Boot test environment.
The @SpringBootTest annotation loads the full application context for integration testing. The @AutoConfigureMockMvc annotation enables and configures MockMvc to perform requests against the application without a running server.
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {
@Autowired
private MockMvc mockMvc;
@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:29-43