Unlocking the Power of Quarkus: A Guide to Migrating Spring Projects Using Gradle

DefineX
TeamDefineX
Published in
7 min readMay 24, 2023

What is Quarkus?

Quarkus is an open-source Java-based framework that enables developers to build cloud-native applications that are fast, lightweight, and efficient. Unlike traditional Java stacks, Quarkus is optimized for containerized environments like Kubernetes, which are essential for modern cloud computing. The framework employs a range of technologies that improve application startup time, minimize memory consumption, and facilitate the efficient running of Java applications. It seamlessly integrates with popular technologies like Apache Kafka, GraphQL, and RESTful web services while also supporting Java frameworks such as Spring and Hibernate. Overall, Java developers can rely on Quarkus to build powerful, versatile, and high-performance cloud-native applications.

Advantages of Quarkus over Spring

While Quarkus and Spring are both popular frameworks for building Java applications, there are some notable differences that set them apart. Here are some of the advantages of Quarkus over Spring:

1. Faster startup time: Quarkus is lightweight and designed for speed, with a startup time that is up to 10 times faster than Spring. This is especially useful for microservices and serverless applications that require fast startup.

2. Lower memory footprint: Quarkus uses less memory footprint than Spring, making it an ideal choice for resource-constrained environments like containers or embedded devices.

3. Native image support: Quarkus provides native image support, allowing your application to be compiled into a standalone executable that can run without a JVM. This can provide significant performance improvements and further reduce memory usage.

4. Support for reactive programming: Quarkus supports reactive programming with tools like Mutiny and Reactive Streams, enabling the development of highly responsive and scalable applications.

5. DevOps integration: Quarkus integrates with popular tools used in the DevOps toolchain, including Kubernetes, simplifying deployment and management in a cloud-native environment.

6. Simplified development experience: Quarkus offers a simplified development experience with features like hot reloading, fast test execution, and easy configuration. This can improve developer productivity and shorten time-to-market for new applications.

7. Cloud-native architecture: Quarkus is designed with cloud-native architecture in mind, with support for technologies like HTTP/2, GraalVM, and Kubernetes. This makes it a great choice for building cloud-native applications that can scale and be deployed anywhere.

The performance improvement is best described in the following figure. Green bars represent the Quarkus using its native GraalVM, while blue bars represent Quarkus using the traditional Java Virtual Machine. Gray bars are the traditional methods. As can be seen, Quarkus provides a significant increase in speed, memory and response times, both in idle and database operations.

Quarkus Performance
Quarkus Performance

For users already familiar with Spring, Quarkus provides some extensions to ease the migration processes. Here is a table showing the Quarkus equivalents of some of the most commonly used Spring components.

Spring — Quarkus Common Components List

Getting Started with the Migration

The rest of this article will guide you through the process of creating and testing a simple Quarkus application using the Spring Web API. We will use the RESTEasy extension for Spring Web migration. Before we get started, there are a few prerequisites:

· An IDE (we will use Intellij from JetBrains)

· JDK 11+ installed with the JAVA_HOME environment variable configured appropriately

· Apache Maven 3.8.6 or a later version installed on your system.

· Optionally, you can use the Quarkus CLI

· If you plan to build a native executable, you can optionally install and configure Mandrel or GraalVM. Alternatively, you can use Docker for native container builds.

Project

To start, we will install Quarkus CLI so that we can create a new project. Since I am running Windows, I will use Chocolatey to install the Quarkus CLI. If you are using a different OS, you can refer to this guide about Quarkus CLI.

To install the Quarkus CLI using Chocolatey, simply run the following command:

choco install quarkus

This will install the latest version of the Quarkus CLI.

Once quarkus is installed, it will be added to your system’s $PATH . To verify the installation was successful, you can run the following command: quarkus –-version . This will print the installed version of the Quarkus CLI, for example:

quarkus --version
2.16.3.Final

If a newer version of the Quarkus CLI is available, you can upgrade it using the following command:

choco upgrade quarkus

To create a new Quarkus starter project with resteasy extension, run the following command. Note that you can remove the — gradle option if you prefer to use maven instead.

quarkus create app org.acme:spring-web-quickstart --extension='spring-web,resteasy-reactive-jackson' --no-code --gradle

Quarkus CLI has created our project, which we can now open in our preferred IDE. We can verify that all the necessary dependencies have been injected by the Quarkus CLI, as shown in the image below.

Adding a Simple Controller

Now let’s create a simple Controller for our Web API. We will need to create a new file called HelloWorldController.java at src/main/java/org/acme/spring/web/. This controller with contain the Spring Web annotations to define our REST endpoint, as follows:

Once the file is created, we can run our application on dev mode using the command:

quarkus dev

Then, we can go to http://localhost:8080/helloworld to see the message displayed.

Improving the controller with more functional endpoints

To improve the controller with more functional endpoints, we can add another endpoint that returns a JSON value.

Adding OpenAPI support and using Swagger UI

Let’s add OpenAPI support and Swagger-UI to our project so that we can easily test our new endpoint. To do this, we will use the quarkus-smallrye-openapi extension.

To add the extension, run the following command:

./gradlew addExtension --extensions='quarkus-smallrye-openapi'

Notice that the OpenAPI dependency has been added to our build.gradle

You can download OpenAPI schema document of your controller by browsing to http://localhost:8080/q/openapi . The following is an example schema document:

---
openapi: 3.0.3
info:
title: spring-web-quickstart API
version: 1.0.0-SNAPSHOT
paths:
/helloworld:
get:
tags:
- Hello World Controller
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
/helloworld/{name}:
get:
tags:
- Hello World Controller
parameters:
- name: name
in: path
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Hello'
components:
schemas:
Hello:
type: object
properties:
message:
type: string

Before we check our endpoints on Swagger UI, let’s improve the appearance of our controller class by adding some OpenAPI annotations to our HelloWorldController class level.

@OpenAPIDefinition(
info = @Info(
title="HelloWorld API",
version = "1.0.1",
contact = @Contact(
name = "HelloWorld API Support",
url = "http://exampleurl.com/contact",
email = "ovuncsupport@example.com"),
license = @License(
name = "Apache 2.0",
url = "https://www.apache.org/licenses/LICENSE-2.0.html"))
)

We can then provide some information about our controller endpoints.

    @Tag(name = "Hello", description = "Just say hello to world")
@GetMapping(produces= MediaType.TEXT_PLAIN_VALUE)
public String helloWorld() {
return "Ovunc says hello world!";
}

@GetMapping(value = "/{name}", produces=MediaType.APPLICATION_JSON_VALUE)
@Tag(name = "Hello by name", description = "Say hello to given name")
public Hello sayHello(@PathVariable(name = "name") String name) {
return new Hello("hello " + name);
}

For more information, you can refer to the official OpenAPI documentation.

When running in Dev or Test mode, Swagger UI is included. In Prod mode, it can be optionally added.

Finally, we can navigate to localhost:8080/q/swagger-ui/ and see the Swagger UI screen:

You can find the source code for this project here on my GitHub account.

In this article, we learned how to create a simple Quarkus application that migrates from Spring’s Web API using Gradle. We added two functional endpoints and customized them to our needs. Lastly, we added OpenAPI and Swagger-UI support for our consumer needs. Until the next article…

Happy Coding!

--

--

DefineX
TeamDefineX

We provide insights for leaders of digital world to accelerate digital transformation and liberate global markets with technology. Visit us at teamdefinex.com.