Quarkus - Creating Your First Application

Learn how to create a Hello World Quarkus app.This guide covers:

  • Bootstrapping an application

  • Creating a JAX-RS endpoint

  • Injecting beans

  • Functional tests

  • Packaging of the application

1. Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 8 or 11+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

Verify Maven is using the Java you expectIf you have multiple JDK’s installed it is not certain Maven will pick up the expected javaand you could end up with unexpected results.You can verify which JDK Maven uses by running mvn —version.

2. Architecture

In this guide, we create a straightforward application serving a hello endpoint. To demonstratedependency injection, this endpoint uses a greeting bean.

Architecture

This guide also covers the testing of the endpoint.

3. Solution

We recommend that you follow the instructions from Boostrapping project and onwards to create the application step by step.

However, you can go right to the completed example.

Download an archive or clone the git repository:

  1. git clone https://github.com/quarkusio/quarkus-quickstarts.git

The solution is located in the getting-started directory.

4. Bootstrapping the project

The easiest way to create a new Quarkus project is to open a terminal and run the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=getting-started \
  4. -DclassName="org.acme.quickstart.GreetingResource" \
  5. -Dpath="/hello"
  6. cd getting-started

It generates the following in ./getting-started:

  • the Maven structure

  • an org.acme.quickstart.GreetingResource resource exposed on /hello

  • an associated unit test

  • a landing page that is accessible on http://localhost:8080 after starting the application

  • example Dockerfile files for both native and jvm modes in src/main/docker

  • the application configuration file

Once generated, look at the pom.xml.You will find the import of the Quarkus BOM, allowing you to omit the version on the different Quarkus dependencies.In addition, you can see the quarkus-maven-plugin responsible of the packaging of the application and also providing the development mode.

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>io.quarkus</groupId>
  5. <artifactId>quarkus-bom</artifactId>
  6. <version>${quarkus.version}</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <build>
  13. <plugins>
  14. <plugin>
  15. <groupId>io.quarkus</groupId>
  16. <artifactId>quarkus-maven-plugin</artifactId>
  17. <version>${quarkus.version}</version>
  18. <executions>
  19. <execution>
  20. <goals>
  21. <goal>build</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>
  26. </plugins>
  27. </build>

If we focus on the dependencies section, you can see the extension allowing the development of REST applications:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-resteasy</artifactId>
  4. </dependency>

4.1. The JAX-RS resources

During the project creation, the src/main/java/org/acme/quickstart/GreetingResource.java file has been created with the following content:

  1. package org.acme.quickstart;
  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. }

It’s a very simple REST endpoint, returning "hello" to requests on "/hello".

Differences with vanilla JAX-RSWith Quarkus, there is no need to create an Application class. It’s supported, but not required. In addition, only one instanceof the resource is created and not one per request. You can configure this using the different *Scoped annotations (ApplicationScoped, RequestScoped, etc).

5. Running the application

Now we are ready to run our application.Use: ./mvnw compile quarkus:dev:

  1. $ ./mvnw compile quarkus:dev
  2. [INFO] --------------------< org.acme:getting-started >---------------------
  3. [INFO] Building getting-started 1.0-SNAPSHOT
  4. [INFO] --------------------------------[ jar ]---------------------------------
  5. [INFO]
  6. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ getting-started ---
  7. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  8. [INFO] skip non existing resourceDirectory /Users/starksm/Dev/JBoss/Quarkus/starksm64-quarkus-quickstarts/getting-started/src/main/resources
  9. [INFO]
  10. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ getting-started ---
  11. [INFO] Changes detected - recompiling the module!
  12. [INFO] Compiling 2 source files to /Users/starksm/Dev/JBoss/Quarkus/starksm64-quarkus-quickstarts/getting-started/target/classes
  13. [INFO]
  14. [INFO] --- quarkus-maven-plugin:<version>:dev (default-cli) @ getting-started ---
  15. Listening for transport dt_socket at address: 5005
  16. 2019-02-28 17:05:22,347 INFO [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
  17. 2019-02-28 17:05:22,635 INFO [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 288ms
  18. 2019-02-28 17:05:22,770 INFO [io.quarkus] (main) Quarkus started in 0.668s. Listening on: http://localhost:8080
  19. 2019-02-28 17:05:22,771 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]

Once started, you can request the provided endpoint:

  1. $ curl -w "\n" http://localhost:8080/hello
  2. hello

Hit CTRL+C to stop the application, but you can also keep it running and enjoy the blazing fast hot-reload.

Automatically add newline with curl -w "\n"We are using curl -w "\n" in this example to avoid your terminal printing a '%' or put both result and next command prompt on the same line.

6. Using injection

Dependency injection in Quarkus is based on ArC which is a CDI-based dependency injection solution tailored for Quarkus' architecture.You can learn more about it in the Contexts and Dependency Injection guide.

ArC comes as a dependency of quarkus-resteasy so you already have it handy.

Let’s modify the application and add a companion bean.Create the src/main/java/org/acme/quickstart/GreetingService.java file with the following content:

  1. package org.acme.quickstart;
  2. import javax.enterprise.context.ApplicationScoped;
  3. @ApplicationScoped
  4. public class GreetingService {
  5. public String greeting(String name) {
  6. return "hello " + name;
  7. }
  8. }

Edit the GreetingResource class to inject the GreetingService and create a new endpoint using it:

  1. package org.acme.quickstart;
  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. import org.jboss.resteasy.annotations.jaxrs.PathParam;
  8. @Path("/hello")
  9. public class GreetingResource {
  10. @Inject
  11. GreetingService service;
  12. @GET
  13. @Produces(MediaType.TEXT_PLAIN)
  14. @Path("/greeting/{name}")
  15. public String greeting(@PathParam String name) {
  16. return service.greeting(name);
  17. }
  18. @GET
  19. @Produces(MediaType.TEXT_PLAIN)
  20. public String hello() {
  21. return "hello";
  22. }
  23. }

If you stopped the application, restart the application with ./mvnw compile quarkus:dev.Then check that the endpoint returns hello quarkus as expected:

  1. $ curl -w "\n" http://localhost:8080/hello/greeting/quarkus
  2. hello quarkus

7. Development Mode

quarkus:dev runs Quarkus in development mode. This enables hot deployment with background compilation, which meansthat when you modify your Java files and/or your resource files and refresh your browser, these changes will automatically take effect.This works too for resource files like the configuration property file.Refreshing the browser triggers a scan of the workspace, and if any changes are detected, the Java files are recompiledand the application is redeployed; your request is then serviced by the redeployed application. If there are any issueswith compilation or deployment an error page will let you know.

This will also listen for a debugger on port 5005. If you want to wait for the debugger to attach before running youcan pass -Dsuspend on the command line. If you don’t want the debugger at all you can use -Ddebug=false.

8. Testing

All right, so far so good, but wouldn’t it be better with a few tests, just in case.

In the generated pom.xml file, you can see 2 test dependencies:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-junit5</artifactId>
  4. <scope>test</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.rest-assured</groupId>
  8. <artifactId>rest-assured</artifactId>
  9. <scope>test</scope>
  10. </dependency>

Quarkus supports Junit 5 tests.Because of this, the version of the Surefire Maven Plugin mustbe set, as the default version does not support Junit 5:

  1. <plugin>
  2. <artifactId>maven-surefire-plugin</artifactId>
  3. <version>${surefire-plugin.version}</version>
  4. <configuration>
  5. <systemProperties>
  6. <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
  7. </systemProperties>
  8. </configuration>
  9. </plugin>

We also set the java.util.logging system property to make sure tests will use the correct logmanager.

The generated project contains a simple test.Edit the src/test/java/org/acme/quickstart/GreetingResourceTest.java to match the following content:

  1. package org.acme.quickstart;
  2. import io.quarkus.test.junit.QuarkusTest;
  3. import org.junit.jupiter.api.Test;
  4. import java.util.UUID;
  5. import static io.restassured.RestAssured.given;
  6. import static org.hamcrest.CoreMatchers.is;
  7. @QuarkusTest
  8. public class GreetingResourceTest {
  9. @Test (1)
  10. public void testHelloEndpoint() {
  11. given()
  12. .when().get("/hello")
  13. .then()
  14. .statusCode(200) (2)
  15. .body(is("hello"));
  16. }
  17. @Test
  18. public void testGreetingEndpoint() {
  19. String uuid = UUID.randomUUID().toString();
  20. given()
  21. .pathParam("name", uuid)
  22. .when().get("/hello/greeting/{name}")
  23. .then()
  24. .statusCode(200)
  25. .body(is("hello " + uuid));
  26. }
  27. }
1By using the QuarkusTest runner, you instruct JUnit to start the application before the tests.
2Check the HTTP response status code and content

These tests use RestAssured, but feel free to use your favorite library.

You can run these using Maven:

  1. ./mvnw test

You can also run the test from your IDE directly (be sure you stopped the application first).

By default, tests will run on port 8081 so as not to conflict with the running application. We automaticallyconfigure RestAssured to use this port. If you want to use a different client you should use the @TestHTTPResourceannotation to directly inject the URL of the test into a field on the test class. This field can be of the typeString, URL or URI. This annotation can also be given a value for the test path. For example, if I want to testa Servlet mapped to /myservlet I would just add the following to my test:

  1. @TestHTTPResource("/myservlet")
  2. URL testUrl;

The test port can be controlled via the quarkus.http.test-port config property. Quarkus also creates a systemproperty called test.url that is set to the base test URL for situations where you cannot use injection.

9. Packaging and run the application

The application is packaged using ./mvnw package.It produces 2 jar files in /target:

  • getting-started-1.0-SNAPSHOT.jar - containing just the classes and resources of the projects, it’s the regularartifact produced by the Maven build;

  • getting-started-1.0-SNAPSHOT-runner.jar - being an executable jar. Be aware that it’s not an über-jar asthe dependencies are copied into the target/lib directory.

You can run the application using: java -jar target/getting-started-1.0-SNAPSHOT-runner.jar

The Class-Path entry of the MANIFEST.MF from the runner jar explicitly lists the jars from the lib directory.So if you want to deploy your application somewhere, you need to copy the runner jar as well as the lib directory.
Before running the application, don’t forget to stop the hot reload mode (hit CTRL+C), or you will have a port conflict.

10. Async

The resource can also use CompletionStage as return type to handle asynchronous actions:

  1. @GET
  2. @Produces(MediaType.TEXT_PLAIN)
  3. public CompletionStage<String> hello() {
  4. return CompletableFuture.supplyAsync(() -> {
  5. return "hello";
  6. });
  7. }

The async version of the code is available in the GitHub repository, in the getting-started-async directory.

11. What’s next?

This guide covered the creation of an application using Quarkus.However, there is much more.We recommend continuing the journey with the building a native executable guide, where you learn about the native executable creation and the packaging in a container.

In addition, the tooling guide document explains how to:

  • scaffold a project in a single command line

  • enable the development mode (hot reload)

  • import the project in your favorite IDE

  • and more