Christopher Anabo
Christopher Anabo
Senior Tech Lead
Christopher Anabo

Notes

Bare Minimum QUARKUS RESTAPI

Bare Minimum QUARKUS RESTAPI

Panache is a feature within Quarkus, a modern Java framework that provides simplified ORM (Object-Relational Mapping) functionalities, reducing redundant code and streamlining database operations. Panache offers two main approaches: the active record pattern, represented by PanacheEntity, and the repository pattern, typically used with PanacheEntityBase.

PanacheEntity is designed for the active record pattern. In this setup, the entity class contains both the data and the associated actions, meaning it includes fields corresponding to database columns as well as methods for managing CRUD (Create, Read, Update, Delete) operations.

PanacheEntityBase serves as the superclass for entities that do not use PanacheEntity's default ID field. It is commonly used in the repository pattern, which separates the domain model (entity) from the persistence logic (repository).

Let's explore how to use PanacheEntityResource, an interface that utilizes PanacheEntity instances for data access and exposes them as resources.

Creating the project

Maven ≥ 3.8.2 and java ≥ 17 are needed.

mvn io.quarkus.platform:quarkus-maven-plugin:3.7.4:create 
  -DprojectGroupId=com.crud 
  -DprojectArtifactId=fastest-crud-ever 
  -Dextensions="quarkus-hibernate-orm-rest-data-panache,quarkus-resteasy-reactive-jsonb,quarkus-jdbc-mysql,smallrye-openapi" 
  -DnoCode 

 

Setting up the model

package com.quarkus.crud.model;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;

@Entity
public class Car extends PanacheEntity {
   private String name;
   private String model;
   private Integer year;
   
   //getter and setter
}

 

Create the REST interface

package com.quarkus.crud;

import com.quarkus.crud.model.Car;
import io.quarkus.hibernate.orm.rest.data.panache.PanacheEntityResource;

public interface CarResource extends PanacheEntityResource {

}

The PanacheEntityResource, facilitates the generation of REST resources by leveraging the interfaces within our application. To generate resources for desired entities and repositories, simply provide a corresponding resource interface.

Configure the data source

quarkus.datasource.db-kind=mysql 
quarkus.datasource.username=root
quarkus.datasource.password=your_password
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/quarkus-crud?useTimezone=true&serverTimezone=UTC
quarkus.hibernate-orm.database.generation=drop-and-create

 

Start the application and see the magic

Run with the following options DEV or NATIVE

./mvnw quarkus:dev

OR

./mvnw install -Dnative

 

Swagger Address http://localhost:8080/q/swagger-ui/#/

Conclusion

Many web applications follow a repetitive pattern of CRUD operations with REST APIs, often resulting in tedious coding tasks. The REST Data with Panache extension simplifies this process by automating the creation of essential CRUD endpoints for your entities and repositories. If you’re working on a straightforward project or a prototype where speed is more important than architectural complexities, using REST with Panache is likely the best choice.