First Hello World Quarkus application
Download and Install Quarkus CLI Tool
In case you already haven't installed Quarkus CLI, you can download it and install its latest version with:
curl -Ls https://sh.jbang.dev | bash -s - trust add https://repo1.maven.org/maven2/io/quarkus/quarkus-cli/
curl -Ls https://sh.jbang.dev | bash -s - app install --fresh --force quarkus@quarkusio
You might get prompted to exit terminal session and create a new one in order to quarkus CLI be on the path file.
Create new project
Next, you can create a new Quarkus project using the quarkus create app
command. Here’s how you can do it:
quarkus create app com.example:hello-world
Then, navigate into the newly created project directory:
cd hello-world
Start application in development mode
for running in development mode execute
quarkus dev
It starts your first application which listens on port 8080 for HTTP requests.
Project skeleton was created with endpoints /
and /hello
.
With your web browser, you check out the endpoints http://localhost:8080/ and http://localhost:8080/hello.
For /hello
endpoint it also makes sense to check it with curl http://localhost:8080/hello
command.
curl http://localhost:8080/hello
Hello from RESTEasy Reactive
Let's examine project structure:
.
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
└── src
├── main
│ ├── docker
│ │ ├── Dockerfile.jvm
│ │ ├── Dockerfile.legacy-jar
│ │ ├── Dockerfile.native
│ │ └── Dockerfile.native-micro
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── GreetingResource.java
│ └── resources
│ ├── application.properties
│ └── META-INF
│ └── resources
│ └── index.html
└── test
└── java
└── com
└── example
├── GreetingResourceIT.java
└── GreetingResourceTest.java
13 directories, 13 files
Static versus dynamic application content
File index.html
holds content for /
endpoint. Feel free to change content of the file
index.html
to
<h1>Hello static content</h1>
for example in the development mode and check the outcome with the browser.
Now, let's have a look at GreetingResource.java
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from RESTEasy Reactive";
}
}
This class defines a resource accessible at the /hello path using the HTTP GET method. It produces a response of type TEXT_PLAIN, and the method returns the string "Hello from RESTEasy Reactive".
Running in Production Mode
Stop quarkus in dev mode in order to make port 8080 available for listening. In terminal, where you started quarkus dev
stop the process with CTRL+C
. To run the application in production mode, first, package the application using Maven:
./mvnw package -Dquarkus.package.type=uber-jar
Then, execute the generated JAR file:
java -jar ./target/hello-world-1.0.0-SNAPSHOT-runner.jar
You should see output like this:
$ java -jar ./target/hello-world-1.0.0-SNAPSHOT-runner.jar
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2024-03-12 21:14:28,912 INFO [io.quarkus] (main) hello-world 1.0.0-SNAPSHOT on JVM (powered by Quarkus 3.8.2) started in 0.913s. Listening on: http://0.0.0.0:8080
2024-03-12 21:14:28,924 INFO [io.quarkus] (main) Profile prod activated.
2024-03-12 21:14:28,924 INFO [io.quarkus] (main) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx]
Summary
In this tutorial, we've covered the essentials of setting up and working with Quarkus, a powerful Java framework designed for building cloud-native applications. We started by installing the Quarkus CLI tool and creating a new Quarkus project. Exploring the project structure, we understood the layout and key files. We then ran the application in development mode, where we could make changes and see them reflected instantly thanks to hot reload. We examined serving static content and defining RESTful endpoints using Quarkus annotations. Finally, we transitioned to production mode, packaging and executing our application.