A simple Rest API using Quarkus

The Java ecosystem is a good example of how a technology can reinvent itself.
For example, it is interesting to reflect on how Spring Boot brought the Java community features that made the language use among the most widely used in the world for new projects.

But now, there are so many innovative and important projects that are designed from the ground up to run on cloud and with microservice architectures.

In this case, many new projects are programming language agnostic and an important factor is taken into consideration when choosing technologies in new projects. The cost.

Java is proven to be a fantastic technology with an active community, but has lost ground in many projects where Python or NodeJS (for example) made cloud costs cheaper due to their lower CPU and memory consumption.

Thinking of making Java relevant and still strong in Cloud Native applications, the Quarkus project was launched.
This is a Red Hat initiative and you can visit the official website here.
If you want know more about Quarkus with a point view of Red Hat, see here.

In this article, let’s be more practical and create simple Rest Get and Post endpoints.

You will need:

  • An IDE;
  • JDK 1.8+ installed with JAVA_HOME configured appropriately;
  • Apache Maven 3.5.3+
  • Postgres 11+

Choose a directory of your choice on your operating system, in my case I used one called init-quarkus.
Open a command line terminal and enter the directory you created.

Let`s start.

Use the following command to create your project.

mvn io.quarkus:quarkus-maven-plugin:0.21.1:create \
-DprojectGroupId=my-groupId \
-DprojectArtifactId=my-artifactId \
-DprojectVersion=my-version \
-DclassName="org.my.group.MyResource"

If you want, replace the name of GroupId and ArtifactId for your favorite name.

Now, let’s add the necessary extensions or dependencies.
$ mvn quarkus:add-extension -Dextensions="quarkus-hibernate-orm-panache"

$ mvn quarkus:add-extension -Dextensions="quarkus-resteasy-jackson"

In your IDE, import the new Maven project (located in the folder you created).

Find in your project the file GreetingResource.java.

This file is default generated when you create the project. The method public String hello() also is default generated.
Follow this file, see the annotation @Path(“/init”). You will use this path in you first request for test.
You can make request http://localhost:8080/init on your browser.
If everthing it’s ok, you browser returns this message:
hello, this is begin with Quarkus!!!

Some comments about this code (GreetingResource.java):

  • Annotation @ApplicationScoped
    By default in the Quarkus framework, it will help create the communication context between the controlling classes and the business rule handling classes. Our class of service will also have this same annotation.
  • Annotation @Path
    The root path for your Rest API.
  • @Produces(MediaType.APPLICATION_JSON) and @Consumes(MediaType.APPLICATION_JSON)
    The type of data that your API will use for receive and send information.
  • @Inject private ProductService productService
    Injection of Dependency for your project. We still go create the class ProductService.
  • Methods addProduct (with POST) and getProduct (with GET)
    Controller methods for communicate with your services. POST add information in your database and GET for request information of database.

Now, we go create our classes Service, Repository and Entity.

ProductService.java

ProductRepository.java

See the code implements PanacheRepository<Product>.
Here, we used the extension of Panache that added in the begin of this article for work our data.

Product.java

For the setup database, first you can create the database with name DB-PRODUCT and after you can configure the file application.properties in src/main/resources :

# Postgres Connection

quarkus.datasource.url=jdbc:postgresql://localhost:5432/DB-PRODUCT
quarkus.datasource.driver=org.postgresql.Driver
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres

And in your pom.xml:

    <!-- JDBC driver dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>

Execute the scripts in your database:

CREATE TABLE product (
ID bigint,
name_product varchar(80),
sector_product varchar(80),
brand_product varchar(80),
price_product float
);

insert into product (
id,
name_product,
sector_product,
brand_product,
price_product
)
values (1,’Corolla’,’Cars’,’Toyota’,10000);

ProductService class considerations:

  • This class is part of the context and so also uses @ApplicationScoped;
  • We also use the default dependency injection of Quarkus with @Inject to use the repository class;
  • In the addProduct method we use annotation @Transactional. This annotation is important when entering or updating information.

ProductRepository class considerations:

We already do here in this class the implementation of Hibernate ORM in a simplified way with JPA. What makes this part simpler is our hibernate-panache extension.

Entity class considerations:

Implementation of extends PanacheEntityBase.
This is our Hibernate ORM in a simplified way with JPA. What makes this part simpler is our hibernate-panache extension.

Testing our API:

Start your application:

In your folder of project, execute:
mvn compile quarkus:dev

You can use a tool like Postman to make your requests.

First let’s try:
Request GET — http: //localhost /8080/init/products

This should return something like:

[{"name":"Corolla","sector":"Cars","brand":"Toyota","price":10000.0,"persistent":true})]

After let’s try:
Request POST — http://localhost/8080/init/add/products

With Body: {“name”:”Etios”,”sector”:”Cars”,”brand”:”Toyota”,”price”:11000.0}

Verify on your database if your request POST added this new informations:

SELECT * FROM product;

1;”Corolla”;”Cars”;”Toyota”;10000
2;”Etios”;”Cars”;”Toyota”;11000

The full code of this article is in my Github:
https://github.com/vinicius-pereiradeoliveira/init-quarkus

Thank’s for read this article.

Visit my page for online courses:

https://learncodeclub.systeme.io/

--

--

--

Software Developer || Technology Researcher || Very interested in economics and general aspects of society

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Spring Boot — Spring framework(下)

.NET Core AWS Lambda Multiple Function in Single Project

How I’m learning programming at 29

Istio-Proxy Ingress SSL Certificate Integration the old fashioned* way(no SDS)

Caching API Calls with Redis to Work Remotely

Create placeholder data with GeoPoint and Date values, and upload it to Firebase Firestore using…

Task-3 :Creating a web portal for our company with all the security as much as possible.

AWS Firelens demystified

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinícius Pereira de Oliveira

Vinícius Pereira de Oliveira

Software Developer || Technology Researcher || Very interested in economics and general aspects of society

More from Medium

Multiple JDK / Java env setup on Mac

Helidon 2 CLI

RabbitMQ with Java, Spring and Docker, asynchronous communication between microservices

How To Manage Multi JAVA Versions On Mac