QUARKUS + Hibernate CRUD example - Creating a CRUD REST API/Service

Hello everyone, today we will discuss how to build RESTful API using QUARKUS with the help of one simple example.


Quarkus is a Java framework designed to run within containers. Fixating on expeditious start-up times and low memory utilization making it more felicitous to run within container orchestration platforms like Kubernetes.Quarkus supports many industry-standard libraries such as Hibernate, Kubernetes, RESTEasy, and Eclipse MicroProfile.

It uses JAX-RS for the REST endpoints, JPA to preserve data models, and CDI for dependency injections.

 

Let's start to build RESTful API with QUARKUS

 Technologies used:

  • Quarkus 
  • Hibernate 
  • H2 Database
  • Java 8

 

Project Directory


 
Maven Dependencies[pom.xml]

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.knf.dev</groupId>
<artifactId>quarkus-rest-api-demo</artifactId>
<version>1.0</version>

<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<quarkus-plugin.version>1.3.0.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom
</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.3.0.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemProperties>
<java.util.logging.manager>
org.jboss.logmanager.LogManager
</java.util.logging.manager>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>

 

Entity class(User.class) - ORM

package com.knf.dev;

import javax.persistence.*;
import java.io.Serializable;

@Table(name = "users")
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String emailId;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public User(Long id, String firstName, String lastName, String emailId) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}

public User() {
super();
}

}

 

UserEndpoint.class

package com.knf.dev;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/api/users")
public class UserEndpoint {

@Inject
UserResource userResource;

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers() {
return userResource.getUsers();
}

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("id") Long id) {
return userResource.getUser(id);
}

@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void updateUser(@PathParam("id") Long id, User user) {
userResource.updateUser(id, user);
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public User addUser(User user) {
return userResource.addUser(user);
}

@DELETE
@Path("/{id}")
public void deleteUser(@PathParam("id") Long id) {
userResource.deleteUser(id);
}
}

 

UserResource.class

package com.knf.dev;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;

@Singleton
public class UserResource {

@Inject
EntityManager entityManager;

public List<User> getUsers() {
return entityManager.createQuery("SELECT c FROM User c").
getResultList();
}

public User getUser(Long id) {
return entityManager.find(User.class, id);
}

@Transactional(Transactional.TxType.REQUIRED)
public User addUser(User user) {
entityManager.persist(user);
return user;
}

@Transactional(Transactional.TxType.REQUIRED)
public void updateUser(Long id, User user) {
User userToUpdate = entityManager.find(User.class, id);
if (null != userToUpdate) {
userToUpdate.setFirstName(user.getFirstName());
userToUpdate.setLastName(user.getLastName());
userToUpdate.setEmailId(user.getEmailId());
} else {
throw new RuntimeException("No such user available");
}
}

@Transactional(Transactional.TxType.REQUIRED)
public void deleteUser(Long id) {
User user = getUser(id);
entityManager.remove(user);
}
}

 
application.properties

# datasource configuration
quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
# quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:h2:mem:test

# drop and create the database at startup (use `update` to only update the
schema)
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.sql-load-script = data.sql

 
data.sql [Dummy data]

INSERT INTO users (id, firstName, lastName, emailId) VALUES
(19871343, 'Sibin', 'vaheed', 'sibinmuhammed@gmail.com');

INSERT INTO users (id, firstName, lastName, emailId) VALUES
(3434419871, 'Sabin', 'vaheed', 'sibinmuhammed@gmail.com');

 

Verify REST APIs

Build application  jar file: mvn clean package

Start application in jetty server: java -jar target/quarkus-rest-api-demo-1.0-runner.jar

You can test APIs using an API tester, We are using POSTMAN

1.Get all users


 

2.Get single user


 

3.Add user


 

4.Update user


 

5.Delete user



Download Source code

git clone: https://github.com/knowledgefactory4u/Java-Framework-web.git

Popular posts from this blog

Spring boot video streaming example-HTML5

DataTable-Pagination example with Spring boot, jQuery and ajax

10 Best Job Posting Sites 2021-2022

Spring boot web project free download:User Registration System

Spring Boot + JPA/Hibernate One to Many mapping example

5 Hardest Puzzle,100% fail answers

Java security AES,SHA256,SHA512,MD5-Spring Boot Project Free Download

Spring Boot-AngularJS-Bootstrap-JPA-CRUD

ReactJS - Bootstrap - Buttons

Spring Boot file upload/download example