Quarkus - Demo Application

Quarkus - Demo Application

Creating first Quarkus app

Let's create an App which will recommend random quotes from a database. To start , let us generate an app using maven CLI .

mvn io.quarkus.platform:quarkus-maven-plugin:2.10.0.Final:create \
    -DprojectGroupId=dev.pallav \
cd quotes

To run the above project we can run the simple command and it will start the server at 8080 port.

./mvnw quarkus:dev

The above step has generated a GreetingResource as default which has an endpoint /hello GreetingController has below code .

public class GreetingResource {  

  public String hello() {  
  return "Hello from RESTEasy Reactive";  
  • @Path is annotation for for describing URL path
  • @GET - invokes the GET endpoint
  • @Produces - Response type, here its a plain text, other example could be JSON or XML

We can use postman or curl to access the endpoint and validate if it works.

[codingsaint@fedora ~]$ curl http://localhost:8080/hello
Hello from RESTEasy Reactive

This showed how easy was it to create a working project with Quarkus.

Injection of the service

Let us add more details. We will create QuotesResource will generate random quotes , to start with we will have 5 quotes and let it be picked randomly. We will evolve our system to learn event driven microservices. Our Quote object will be a simple record with author and quote

package dev.pallav.models;  

public record Quote(String author, String quote) {  

Let's create QuoteService

package dev.pallav.service;  

import dev.pallav.models.Quote;  

import javax.enterprise.context.ApplicationScoped;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.Random;  

public class QuoteService {  
  private List<Quote> quotes= new ArrayList<>();  

    public QuoteService(){  
  Quote q1= new Quote("Yoda", "Do, or do not. There is no try");  
        Quote q2= new Quote("Ralph Waldo Emerson", "Happiness is a perfume you cannot pour on others without getting a few drops on yourself");  
        Quote q3= new Quote("Oliver Holmes", "A man may fulfil the object of his existence by asking a question he cannot answer, and attempting a task he cannot achieve.");  
        Quote q4= new Quote("Napoleon Hill", "Opportunity often comes disguised in the form of misfortune, or temporary defeat");  
        Quote q5= new Quote("Mahatma Gandhi", "Our greatness lies not so much in being able to remake the world as being able to remake ourselves");  

  public Quote quote(){  
  Random random= new Random();  
        return quotes.get(random.nextInt(quotes.size()));  

  • @ApplicationScoped - Let container know that bean is application scoped
  • QuoteService constructor - initialize List of Quotes acting as temporary database
  • quote method pick a random quote Since we want a RESTful endpoint with json output , let's add jackson support with dependency in pom.xml .
<!-- Jackson -->  

Not we can create the endpoint and invoke QuoteService to get a random quote.

package dev.pallav;  

import dev.pallav.models.Quote;  
import dev.pallav.service.QuoteService;  

import javax.print.attribute.standard.Media;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  

public class QuoteResource {  

  private final QuoteService quoteService;  

    public QuoteResource(QuoteService quoteService) {  
  this.quoteService = quoteService;  

  public Quote quote(){  
  return quoteService.quote();  
  • You can see the MediaType.APPLICATION_JSON with @Produces annotation to let endpoint produce a JSON response Let's run the application and validate our service, We can curl to validate or use any client as postman
    curl http://localhost:8080/quote
    {"author":"Ralph Waldo Emerson","quote":"Happiness is a perfume you cannot pour on others without getting a few drops on yourself"}

In this article we created a Sample Quarkus Application and learn how to inject (Constructor Injection) a service . In next article we will add a database support and a POST endpoint to add quotes at database.

Did you find this article valuable?

Support Kumar Pallav by becoming a sponsor. Any amount is appreciated!