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:
io.quarkus.runtime.configuration.ConfigurationException: Failed to load config value of type class java.lang.String for: greeting
at io.quarkus.arc.runtime.ConfigRecorder.validateConfigProperties(ConfigRecorder.java:70)
at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigValues1665125174.deploy_0(Unknown Source)
at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigValues1665125174.deploy(Unknown Source)
at io.quarkus.runner.ApplicationImpl.doStart(Unknown Source)
at io.quarkus.runtime.Application.start(Application.java:101)
at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:111)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:71)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:44)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:124)
at io.quarkus.runner.GeneratedMain.main(Unknown Source)
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 -w '\n' localhost:8080/hello
Hello y'all!
Update your test
Since we changed the output of our /hello
endpoint, our 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 continuous 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:
[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: