Microservices with Quarkus

Reading Time: 2 minutes

We won’t go over too much of the basics of microservices in Java. However, after reading this blog, you should understand how to create a standards-based microservice using the Quarkus framework.

If you’re considering migrating an existing service to Quarkus, you’ll get an introduction to how to support some of the basic features, functionality, and configuration options of microservices that you’re probably already used to.

Get Started with Microservices

If you use any of the available options to start a Quarkus project, you’re well on your way to creating a microservice.

you can get bootstrapped with the Quarkus starter at code.quarkus.io or a maven command:

mvn io.quarkus:quarkus-maven-plugin:1.5.0.Final:create -

DprojectGroupId=com.apress.samples -DprojectArtifactId=codewith-quarkus -

DprojectVersion=1.0.0-SNAPSHOT -DclassName=org.acme.ExampleResource -

Dpath=/hello

and you can start it in dev mode

mvn quarkus:dev

You can then hit your microservice with your preferred testing tool. We can use Postman or you could also just hit the sample endpoint in a browser: http://localhost:8080/hello

The Java API for RESTful Web Services, or JAX-RS, is a Java standard for building RESTful Web services. It defines a standard set of annotations and classes that you use to implement RESTful or microservices in Java.

The expectation is that software vendors then implement concrete libraries and frameworks based on the standard.

Quarkus supports the JAX-RS API by way of the RESTEasy library from Red Hat; you can expect to use almost all the functionality recommended by the JAX-RS specification

Annotations in Quarkus

JAX-RS provides annotations, interfaces, and packaged components to handle all of the probable use cases you’ll encounter while building microservices, some of them are –

  • @Path to designate a class or a method as a web API endpoint
  • @Consumes and @Produces to designate what data formats (JSON, XML, etc.) a web API resource accepts and returns, respectively
  • The HTTP method annotations for handling different HTTP request types, from the javax.ws.rs package
    • @GET for GET requests
    • @POST for POST requests
    • @PUT for PUT requests
    • @PATCH for PATCH requests
    • @DELETE for DELETE requests
    • @HEAD for HEAD requests
  • The microservice request parameter handling annotations

Explanation of Bootstrap Code

The ExampleResource that was generated with the bootstrapping provides a small sample that you can get going immediately:

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 ExampleResource {

 @GET

 @Produces(MediaType.TEXT_PLAIN)

 public String hello() {

 return "hello";

 }

}

What you get out of the box is an HTTP endpoint that allows a client to send an HTTP GET request and receive the simple “Hello” string as the response. JAX-RS can do a lot more. Here’s what’s going on here:

  • @Path defines the root URL “hello”, at which the web service is available, relative to the deployment. Therefore, for a project on your personal computer, you can access this web service at http://localhost:8080/project-name/hello.
  • @GET designates that this method can be accessed using an HTTP GET request method, making it a REST resource; you can access it via a browser URL load. It will not respond to any other HTTP method. Because this method doesn’t carry its own @Path annotation, it’s going to default to the @Path annotation set at the class level.
  • @Produces defines the content type that this REST resource method will respond with. The MediaType enum contains all valid HTTP response types.

References: https://quarkus.io/

Knoldus Blogs: https://blog.knoldus.com/

Written by 

Prakhar is a Software Consultant at Knoldus . He has completed his Masters of Computer Applications from Bharati Vidyapeeth Institute of Computer Applications and Management, Paschim Vihar . He likes problem solving and exploring new technologies .

Leave a Reply