"Mastering Java Quarkus: A Hands-On Example"

Getting Started with Java Quarkus: A Comprehensive Example

Java Cheatsheet for Beginners | Learn Java Basics in One Page
Java Cheatsheet for Beginners | Learn Java Basics in One Page

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.

Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀

Setting Up the Development Environment

Before we dive into the example, ensure you have the following tools installed:

Top 50 Java Projects
Top 50 Java Projects
  • Java Development Kit (JDK) 11 or later
  • Apache Maven 3.6 or later
  • Docker (optional, for containerization and deployment)

Creating a New Quarkus Project

Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes

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

Relational Operators in Java beginner friendly and easy to learn 🤩
Relational Operators in Java beginner friendly and easy to learn 🤩

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

an image of a computer screen with many lines on the screen and text below it
an image of a computer screen with many lines on the screen and text below it

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:

Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Complete Java Cheat Sheet for Beginners to Advanced Developers
Complete Java Cheat Sheet for Beginners to Advanced Developers
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners
Java Generics Explained
Java Generics Explained
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
Java Cheat Sheet for GTU Students | Quick Revision Notes & OOP Concepts
Java Cheat Sheet for GTU Students | Quick Revision Notes & OOP Concepts
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
Java Data Types Explained
Java Data Types Explained
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Interface in Java
Interface in Java
🚀 Master Java Arithmetic Operators | Java Basics for Beginners
🚀 Master Java Arithmetic Operators | Java Basics for Beginners
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
Arrays in Java Explained | Java Programming Basics for Beginners
Arrays in Java Explained | Java Programming Basics for Beginners

```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!