The application
This lesson uses an existing application, https://github.com/RedHat-Middleware-Workshops/spring-jkube-external-config, as its starting point. It is a simple Spring Boot application using Spring MVC to expose a RESTful endpoint. It additionally exposes the Spring Boot Actuator endpoints, including the Kubernetes probes, health, info, and env.
Examine the application
The main branch contains the starting point for this lesson. Please download/clone it to proceed. The solution branch contains the application as it should look at the end of this lesson.
Let’s examine the main branch’s contents:
pom.xml- The Maven build file for the project.
src/main/java/org/acme/externalconfig/rest/HelloController.java- The Spring MVC class exposing the
/helloREST endpoint. - Injects the
hello.greetingproperty and uses it in the response of the/helloendpoint.
- The Spring MVC class exposing the
src/main/resources/application.yml- Configuration for the application, including a default value for the
hello.greetingproperty used byHelloController.
- Configuration for the application, including a default value for the
src/test/java/org/acme/externalconfig/rest/HelloControllerTests.java- Tests for the
/helloendpoint exposed in theHelloControllerclass.
- Tests for the
Run the application locally
Let’s run the application locally to become familiar with it. Open a terminal to the project’s root directory and execute ./mvnw clean package spring-boot:run on Linux/macOS or mvnw.cmd clean package spring-boot:run on Windows. This will compile the application, run all the tests, and then expose the HTTP endpoints on port 8080.
Now, let’s try interacting with the endpoints by opening a browser window or using the curl command from another terminal. The following shows what the values of some of the endpoints should look like:
- http://localhost:8080/hello
Hello World
- http://localhost:8080/hello/Yoda
Hello Yoda
- http://localhost:8080/actuator/info
{}
- http://localhost:8080/actuator/env/hello.greeting
{
"property": {
"source": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'",
"value": "Hello"
}
}
NOTE: There is most likely more data in the http://localhost:8080/actuator/env/hello.greeting response. The output is trimmed for brevity.
The /actuator/env/hello.greeting endpoint is the Spring Boot Actuator Environment endpoint. It allows retrieving information about the entire environment or just a single property. This information includes the current value of a property and where it was resolved.
In our case, you can see that the current value of hello.greeting is Hello, and it was resolved from the application.yml file on the classpath, which originated in src/main/resources/application.yml.
Stop the application using CTRL-C before moving to the next section.