Hello World

Introduction

In this step, you will create a straightforward application serving a hello endpoint. To demonstrate dependency injection this endpoint uses a greeting bean.

 
Image
Greeting Service

 

Creating a basic project

The easiest way to create a new Quarkus project is to run the following maven command:
mvn io.quarkus:quarkus-maven-plugin:1.12.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-DclassName="org.acme.quickstart.GreetingResource" \
-Dpath="/hello"

This will use the Quarkus Maven Plugin and generate a basic Maven project for you in the getting-started subdirectory, generating:

  • The Maven structure
  • An org.acme.quickstart.GreetingResource resource exposed on /hello
  • An associated unit test
  • A landing page that is accessible at http://localhost:8080 after starting the application
  • Example Dockerfiles for a variety of build targets (native, jvm, etc)
  • The application configuration file

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

<dependencyManagement>
  <dependencies>
    <dependency>
    <groupId>${quarkus.platform.group-id}</groupId>
    <artifactId>${quarkus.platform.artifact-id}</artifactId>
    <version>${quarkus.platform.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
If we focus on the dependencies section, you can see we are using Quarkus extensions allowing the development and testing of REST applications:
<dependencies>
  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-arc</artifactId>
  </dependency>
  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-resteasy</artifactId>
  </dependency>
  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
During the project creation, the getting-started/src/main/java/org/acme/quickstart/GreetingResource.java file has been created with the following endpoint:

 

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

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello RESTEasy";
    }
}
It’s a very simple REST endpoint, returning "hello" to requests on /hello.

 

Note: Compared to vanilla JAX-RS, with Quarkus there is no need to create an `Application` class. It’s supported but not required. In addition, only one instance of the resource is created instead of one per request. You can configure this using the different `*Scoped` annotations (`ApplicationScoped`, `RequestScoped`, etc.).

 

Running the Application

From within the project directory (the getting-started directory created by the maven call above), run the application using:  

mvn compile quarkus:dev -Dquarkus.http.host=0.0.0.0
After the build is complete, you should see:

 

Listening for transport dt_socket at address: 5005
__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-04-26 09:48:07,142 INFO  [io.quarkus] (Quarkus Main Thread) getting-started 1.0.0-SNAPSHOT on JVM (powered by Quarkus 1.12.2.Final) started in 1.176s. Listening on: http://0.0.0.0:8080
2021-04-26 09:48:07,145 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-04-26 09:48:07,145 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]
Open a new terminal window and run the following command to query the application:

 

curl localhost:8080/hello

 

The application should respond with the following:

 

Hello RESTEasy

Live Reloading

 

Next, let's exercise the live reload capabilities of Quarkus. Edit the source code at src/main/java/org/acme/quickstart/GreetingResource.java and change line 14 from:

 

Hello RESTEasy

 

to

 

Hola RESTEasy

 

Save the file, but don't restart the running Quarkus process in the first terminal. Run the curl command again:

 

curl localhost:8080/hello

 

This time, the call will return:

 

Hola RESTEasy
The quarkus:dev maven target runs Quarkus in development mode. This enables live reload with background compilation, which means that when you modify your Java files your resource files, these changes will automatically take effect. This will also listen for a debugger on port 5005. If your want to wait for the debugger to attach before running you can pass -Ddebug on the command line. If you don’t want the debugger at all, you can use -Ddebug=false.

 

The Dev UI

 

When running in Developer mode, Quarkus apps expose a useful UI for inspecting and making on-the-fly changes to the app (much like live coding mode). It allows you to quickly visualize all the extensions currently loaded, see and edit their configuration values, see their status and go directly to their documentation.

 

Open the following URL in a broswer to access the Dev UI for your running application:

 

http://localhost:8080/q/dev/
The "Config Editor" link under the "Configuration" section is used to make live updates to the application's configuration. This is super useful for developers to confirm code and configuration changes, or experiment with various settings.

 

Note: The Dev UI is only enabled when in developer mode. It is not deployed when in production mode, as it's designed for developers to use during development. For more detail on what you can do, check out the [Dev UI Guide](https://quarkus.io/guides/dev-ui).

 

Next Steps

 

You've seen how to build a basic app, package it as an executable JAR and start it up very quickly. We'll leave the app running and rely on hot reload for the next steps.

 

In the next step we'll inject a custom bean to showcase Quarkus' CDI capabilities.