Configuration

Hardcoded values in your code is a no go, so let’s see how to add configuration to your application. Quarkus relies on the MicroProfile Config specification and the main configuration file is application.properties.

Using a config property in your code

Let’s change our GreetingResource class to use a configuration property. Change its content to:

package com.redhat.developers;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @ConfigProperty(name = "greeting")
    String greeting;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return greeting;
    }
}

If you refresh your browser pointing to http://localhost:8080/hello, you should see an error with a stacktrace like this:

jakarta.enterprise.inject.spi.DeploymentException: No config value of type [java.lang.String] exists for: greeting
	at io.quarkus.arc.runtime.ConfigRecorder.validateConfigProperties(ConfigRecorder.java:37)
	at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigProperties61.deploy_0(ConfigBuildStep$validateConfigProperties61.zig:120)
	at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigProperties61.deploy(ConfigBuildStep$validateConfigProperties61.zig:36)
	at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:166)
	at io.quarkus.runtime.Application.start(Application.java:89)
	at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:90)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
	at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:99)
	at java.base/java.lang.Thread.run(Thread.java:834)

Which is expected, since we didn’t provide the greeting property neither at the application.properties file nor with some runtime configuration.

Adding a config property to application.properties

Since we requested a greeting property in our code, let’s provide a value to it in our application.properties file available in src/main/resources:

# Configuration file
# key = value
greeting=Hello y'all!

Refresh your browser pointing to http://localhost:8080/hello. You should see the Hello y’all! string.

You can also use curl to check the same result:

curl localhost:8080/hello
Hello y'all!

Update your integration test

Since we changed the output of our /hello endpoint, our integration test doesn’t pass anymore.

Let’s update GreetingResourceTest to reflect our recent changes:

package com.redhat.developers;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
public class GreetingResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/hello")
          .then()
             .statusCode(200)
             .body(is("Hello y'all!"));
    }

}
If you’re still running in live testing mode you will already see that the tests pass again and you can ignore the rest of this page and move on to the next step.

Stop your current Live Coding session of Quarkus in the terminal by sending a CTRL+C:

^C2020-05-11 08:33:41,865 INFO  [io.quarkus] (Quarkus Main Thread) tutorial-app stopped in 0.007s

Now run your tests to check if everything is ok:

  • Maven

  • Quarkus CLI

./mvnw clean test
quarkus test
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.851 s
[INFO] Finished at: 2020-05-11T21:31:31-04:00
[INFO] ------------------------------------------------------------------------

BUILD SUCCESS! You can now go back to the amazing Live Coding mode of Quarkus:

  • Maven

  • Quarkus CLI

./mvnw quarkus:dev
quarkus dev