Quarkus - Accessing application properties with Spring Boot properties API

If you prefer to use Spring Boot @ConfigurationProperties annotated class to access application properties instead of a Quarkus native @ConfigProperties or a MicroProfile @ConfigProperty approach, you can do that with this extension.

This technology is considered preview.

In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require to change configuration or APIs and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.6.2+

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: git clone [https://github.com/quarkusio/quarkus-quickstarts.git](https://github.com/quarkusio/quarkus-quickstarts.git), or download an archive.

The solution is located in the spring-boot-properties-quickstart directory.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=spring-boot-properties-quickstart \
  4. -DclassName="org.acme.spring.boot.properties.GreetingResource" \
  5. -Dpath="/greeting" \
  6. -Dextensions="spring-boot-properties"
  7. cd spring-boot-properties-quickstart

This command generates a Maven project with a REST endpoint and imports the spring-boot-properties extension.

If you already have your Quarkus project configured, you can add the spring-boot-properties extension to your project by running the following command in your project base directory:

  1. ./mvnw quarkus:add-extension -Dextensions="spring-boot-properties"

This will add the following to your pom.xml:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-spring-boot-properties</artifactId>
  4. </dependency>

GreetingController

The Quarkus Maven plugin automatically generated a GreetingResource JAX-RS resource in the src/main/java/org/acme/spring/boot/properties/GreetingResource.java file that looks like:

  1. package org.acme.spring.boot.properties;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. @Path("/hello")
  7. public class GreetingResource {
  8. @GET
  9. @Produces(MediaType.TEXT_PLAIN)
  10. public String hello() {
  11. return "hello";
  12. }
  13. }

Injecting properties

Create a new class src/main/java/org/acme/spring/boot/properties/GreetingProperties.java with a message field:

  1. package org.acme.spring.boot.properties;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties("greeting")
  4. public class GreetingProperties {
  5. public String text;
  6. }

Here text field is public, but it could also be a private field with getter and setter or just a public getter in an interface. Because text does not have a default value it is considered required and unless it is defined in a configuration file (application.properties by default) your application will fail to start. Define this property in your src/main/resources/application.properties file:

  1. # Your configuration properties
  2. greeting.text = hello

Now modify GreetingResource to start using the GreetingProperties:

  1. package org.acme.spring.boot.properties;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path("/greeting")
  8. public class GreetingResource {
  9. @Inject
  10. GreetingProperties properties;
  11. @GET
  12. @Produces(MediaType.TEXT_PLAIN)
  13. public String hello() {
  14. return properties.text;
  15. }
  16. }

Run the tests to verify that application still functions correctly.

Package and run the application

Run the application with: ./mvnw compile quarkus:dev. Open your browser to http://localhost:8080/greeting.

Changing the configuration file is immediately reflected.

As usual, the application can be packaged using ./mvnw clean package and executed using the -runner.jar file. You can also generate the native executable with ./mvnw clean package -Pnative.

Default values

Now let’s add a suffix for a greeting for which we’ll set a default value.

Properties with default values can be configured in a configuration file just like any other property. However, the default value will be used if the property was not defined in a configuration file.

Go ahead and add the new field to the GreetingProperties class:

  1. package org.acme.spring.boot.properties;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties("greeting")
  4. public class GreetingProperties {
  5. public String text;
  6. public String suffix = "!";
  7. }

And update the GreetingResource and its test GreetingResourceTest:

  1. package org.acme.spring.boot.properties;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path("/greeting")
  8. public class GreetingResource {
  9. @Inject
  10. GreetingProperties properties;
  11. @GET
  12. @Produces(MediaType.TEXT_PLAIN)
  13. public String hello() {
  14. return properties.text + properties.suffix;
  15. }
  16. }
  1. package org.acme.spring.boot.properties;
  2. import io.quarkus.test.junit.QuarkusTest;
  3. import org.junit.jupiter.api.Test;
  4. import static io.restassured.RestAssured.given;
  5. import static org.hamcrest.CoreMatchers.is;
  6. @QuarkusTest
  7. public class GreetingResourceTest {
  8. @Test
  9. public void testHelloEndpoint() {
  10. given()
  11. .when().get("/greeting")
  12. .then()
  13. .statusCode(200)
  14. .body(is("hello!"));
  15. }
  16. }

Run the tests to verify the change.

Optional values

Properties with optional values are the middle-ground between standard and properties with default values. While a missing property in a configuration file will not cause your application to fail, it will nevertheless not have a value set. We use java.util.Optional type to define such properties.

Add an optional name property to the GreetingProperties:

  1. package org.acme.spring.boot.properties;
  2. import java.util.Optional;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. @ConfigurationProperties("greeting")
  5. public class GreetingProperties {
  6. public String text;
  7. public String suffix = "!";
  8. public Optional<String> name;
  9. }

And update the GreetingResource and its test GreetingResourceTest:

  1. package org.acme.spring.boot.properties;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path("/greeting")
  8. public class GreetingResource {
  9. @Inject
  10. GreetingProperties properties;
  11. @GET
  12. @Produces(MediaType.TEXT_PLAIN)
  13. public String hello() {
  14. return properties.text + ", " + properties.name.orElse("You") + properties.suffix;
  15. }
  16. }
  1. package org.acme.spring.boot.properties;
  2. import io.quarkus.test.junit.QuarkusTest;
  3. import org.junit.jupiter.api.Test;
  4. import static io.restassured.RestAssured.given;
  5. import static org.hamcrest.CoreMatchers.is;
  6. @QuarkusTest
  7. public class GreetingResourceTest {
  8. @Test
  9. public void testHelloEndpoint() {
  10. given()
  11. .when().get("/greeting")
  12. .then()
  13. .statusCode(200)
  14. .body(is("hello, You!"));
  15. }
  16. }

Run the tests to verify the change.

Grouping properties

Now we have three properties in our GreetingProperties class. While name could be considered more of a runtime property (and maybe could be passed as an HTTP query parameter in the future), text and suffix are used to define a message template. Let’s group these two properties in a separate inner class:

  1. package org.acme.spring.boot.properties;
  2. import java.util.Optional;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. @ConfigurationProperties("greeting")
  5. public class GreetingProperties {
  6. public Message message;
  7. public Optional<String> name;
  8. public static class Message {
  9. public String text;
  10. public String suffix = "!";
  11. }
  12. }

Here Message properties class is defined as an inner class, but it could also be a top level class.

Having such property groups brings more structure to your configuration. This is especially useful when then number of properties grows.

Because of the additional class, our property names have changed. Let’s update the properties file and the GreetingResource class.

  1. # Your configuration properties
  2. greeting.message.text = hello
  1. package org.acme.spring.boot.properties;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path("/greeting")
  8. public class GreetingResource {
  9. @Inject
  10. GreetingProperties properties;
  11. @GET
  12. @Produces(MediaType.TEXT_PLAIN)
  13. public String hello() {
  14. return properties.message.text + ", " + properties.name.orElse("You") + properties.message.suffix;
  15. }
  16. }

More Spring guides

Quarkus has more Spring compatibility features. See the following guides for more details: