Getting Started with Java Quarkus: A Comprehensive Example

In the rapidly evolving landscape of Java application development, Quarkus has emerged as a powerful, Kubernetes-native framework that optimizes Java for GraalVM and improves developer productivity. This article provides a comprehensive, hands-on example of building a simple REST API using Quarkus, guiding you through the process from project creation to deployment.

Setting Up the Development Environment
Before we dive into the example, ensure you have the following tools installed:

- Java Development Kit (JDK) 11 or later
- Apache Maven 3.6 or later
- Docker (optional, for containerization and deployment)
Creating a New Quarkus Project

Let's create a new Quarkus project using the official Quarkus CLI. If you haven't installed it yet, you can do so by running:
brew tap quarkus/tap && brew install quarkus/tap/quarkus
Now, create a new project called "quarkus-example" with the "REST" extension:
quarkus create app quarkus-example --extension REST
Building the REST API

Navigate to the project directory and build the application using Maven:
cd quarkus-example mvn clean package
Quarkus will generate a simple REST API with a single endpoint at /hello that returns "Hello from REST!"
Adding a Greeting Service

Let's enhance the API by adding a greeting service. Create a new class called GreetingService in the src/main/java/org/acme package:
```java package org.acme; public class GreetingService { public String sayHello(String name) { return "Hello, " + name + "!"; } } ```
Then, update the Application.java file to use the new service:




















```java package org.acme; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Application; @Path("/greeting") public class Application extends Application { private final GreetingService greetingService = new GreetingService(); @GET public String sayHello(@QueryParam("name") String name) { return greetingService.sayHello(name); } } ```
The updated application now accepts a name query parameter and returns a personalized greeting.
Testing the API
Start the application using the following command:
quarkus dev
Once the application is running, you can test the API using HTTPie or any other HTTP client:
http GET http://localhost:8080/greeting?name=World
The response should be:
{
"body": "Hello, World!"
}
Containerizing and Deploying the Application
Quarkus allows you to containerize your application using Docker. Create a new file called Dockerfile in the project root with the following content:
```Dockerfile FROM quarkus/quarkus-app:2.1.1.Final-java11 COPY target/quarkus-app/quarkus-app.jar app.jar ENTRYPOINT ["java", "-jar", "/app/app.jar"] ```
Build and push the Docker image to your preferred registry:
docker build -t quarkus-example . docker push quarkus-example
Finally, deploy the container to your desired environment, such as Kubernetes or a cloud provider.
Conclusion
In this comprehensive example, we explored the process of creating, testing, and deploying a simple REST API using Quarkus. By leveraging Quarkus' productivity features and GraalVM's performance benefits, you can build efficient, modern Java applications. Happy coding!