How to write Rest-Full web services using Quarkus

Reading Time: 3 minutes
In this short blog, we will see What is quarkus, how quarkus works, and how we can create API using quarkus, and at the end we will see how we test API on our local System So, let’s begin.

What is Quarkus?

Quarkus is created to enable Java developers to create applications for a modern, cloud-native world. It is a Kubernetes-native Java framework tailored for GraalVM and HotSpot, crafted from best-of-breed Java libraries and standards.

How Quarkus works?

  • Parse a bunch of config files
  • Scan class paths and classes for annotation, getters, and metadata
  • Build framework metamodel objects
  • Prepare reflection and build proxies
  • Start and open IO, threads, etc

What Quarkus does is to move all the bullet points mentioned above except the last one from runtime to build time and hence very fast to boot. Since Quarkus loads, only the classes that are part of the execution flow the size of the build artifact is very less sometimes half the size of what we are customized to see with other frameworks.

Now let’s understand how we can write the rest full API using quarkus…

What is API?

API stands for application program interface, in simple language API means The way one chunk of code communicates with another chunk of code.” It’s not specific to Java.

For writing an API using quarkus we follow some steps:-

Step -1

First, we need to go to the quarkus site to download the project structure.

once you download the file, the file format is ZIP.

Step -2

You need to Unzip the file and open the file on any suitable IDE.

Unzip file command

tar –tzf <FileName.tar.gz>

Once you open your project on IDE then you will a structure like this.

Step- 3

Once you are able to open your project then the next is to download the required dependency.

For downloading a dependency you just need to hit this command.

./mvnw compile 

Or you can manually add the dependency in the POM.xml file.

    <dependency>

      <groupId>io.quarkus</groupId>

      <artifactId>quarkus-arc</artifactId>

    </dependency>

    <dependency>

      <groupId>io.quarkus</groupId>

      <artifactId>quarkus-resteasy-reactive</artifactId>
   
 </dependency>

Step-4

The next step is to write an API class.

Class name: GreetingResource.java

package org.acme;


import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

@Path("/hello")

public class GreetingResource {

    @GET

    @Produces(MediaType.TEXT_PLAIN)

    public String hello() {

        return "Hello, This is my first REST-API ";
    }
}

this is the class that we need to write now let’s understand all the annotations.

@Path(): Path annotation identifies the URI to which the resource gonna responded.

@GET(): GET annotation defines the type of the request.

@Produces(): Produces annotation is basically used to specify media types.

Step -5

once you completed all the steps next step is to run your application

for that you can use the command :

./mvnw compile quarkus:dev

Once your application is running, you will see a message like (started in 2.467s. Listening on: http://localhost:8080)

Adding Screenshots for more clarity:

Once you got this message that means your application is compile and it’s running on the port localhost:8080

Step6

The next step is to open your browser and hit the URL: http://localhost:8080/

and you will a (Congratulations!) message.

If you see the above message on your screen, it means quarkus is running on your system.

Step7

Now the last step is to hit the endpoint that you created through your code. http://localhost:8080/hello

This image has an empty alt attribute; its file name is image-3.png

As you can see in the above screenshots we are able to hit our endpoint and we are getting the correct message.

That’s it, folks. I hope you liked the blogs. Thanks!

References

https://quarkus.io/guides/

To read more tech blogs, visit Knoldus Blogs.

Written by 

Gaurav srivastav is a Software Consultant working in java domain.