Build a REST API With Just 2 Classes in Java and Quarkus
Quarkus enables a full CRUD API with just two classes using Hibernate ORM with Panache. No controllers or repositories needed — just define two classes, and deploy.
Join the DZone community and get the full member experience.
Join For FreeDevelopers often look for ways to build RESTful APIs with minimal effort while maintaining clean and maintainable code. Quarkus enables a fully functional CRUD (Create, Read, Update, Delete) API using just two classes.
With Hibernate ORM with Panache, Quarkus simplifies entity management, and REST Data with Panache automatically exposes a complete set of RESTful endpoints. This approach reduces the time spent on boilerplate code, allowing developers to focus on business logic rather than infrastructure.
This article demonstrates building a developer management API in Java using Quarkus. We will also cover how to interact with the API using cURL
commands, showcasing the efficiency of this approach.
Why Quarkus?
Quarkus is a lightweight, cloud-native Java framework for performance and developer productivity. It reduces boilerplate code while maintaining the power of Java and Jakarta EE.
We can simplify entity management with Hibernate ORM with Panache, and with REST Data with Panache, we can expose a full CRUD API without writing a controller class!
Why Use Quarkus for REST APIs?
Quarkus is designed for efficiency, productivity, and performance. It provides:
- Minimal code. Automatic REST API generation reduces the need for controllers.
- Fast development. Live coding
quarkus:dev
allows instant feedback. - Optimized for cloud. Lower memory footprint and faster startup times.
- Seamless integration. Works effortlessly with microservices and cloud-native architectures.
With these advantages, developers can quickly build and deploy high-performance applications with minimal effort.
Step 1: Create the Entity
Our API will manage developers with basic attributes like name
, email
, language
, and city
.
We define the entity using PanacheEntity
, which automatically provides an id
field and simplifies data access.
package expert.os.videos.quarkus;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
@Entity
public class Developer extends PanacheEntity {
@Column
public String name;
@Column
public String email;
@Column
public String language;
@Column
public String city;
}
How It Works
- The
@Entity
annotation marks this class as a JPA entity. - Extending
PanacheEntity
automatically provides anid
and built-in CRUD operations. - Each field is mapped to a database column with
@Column
.
Since Quarkus manages the database interactions, no additional repository classes are required.
Step 2: Create the REST API in One Line
Instead of writing a full controller class, we can expose a REST API by creating an interface that extends PanacheEntityResource
.
package expert.os.videos.quarkus;
import io.quarkus.hibernate.orm.rest.data.panache.PanacheEntityResource;
public interface DevelopersResource extends PanacheEntityResource<Developer, Long> {
}
How It Works
By extending PanacheEntityResource<Developer, Long>
, Quarkus automatically generates endpoints for:
- GET
/developer
→ Retrieve all developers - GET
/developer/{id}
→ Retrieve a specific developer - POST
/developer
→ Create a new developer - PUT
/developer/{id}
→ Update an existing developer - DELETE
/developer/{id}
→ Delete a developer
With just two classes, a complete CRUD API is available.
Step 3: Run the Application
Make sure you have Quarkus installed and set up in your project.
Start the Quarkus application with:
./mvnw compile quarkus:dev
Step 4: Test the API With cURL
Once the application is running, we can interact with the API using cURL
.
Create a developer (POST request):
curl -X POST http://localhost:8080/developer \
-H "Content-Type: application/json" \
-d '{
"name": "Alice",
"email": "alice@example.com",
"language": "Java",
"city": "Lisbon"
}'
Get all developers (GET request):
curl -X GET http://localhost:8080/developer
Get a single developer by ID (GET request):
curl -X GET http://localhost:8080/developer/1
Conclusion
Quarkus significantly simplifies building REST APIs in Java, reducing the need for extensive boilerplate code while maintaining flexibility and performance. Using Hibernate ORM with Panache and REST Data with Panache, we could expose a fully functional CRUD API using just two classes — a domain entity and a resource interface.
This approach eliminates the need for manually implementing repository classes or defining explicit REST controllers, allowing developers to focus on business logic rather than infrastructure. The result is a more concise, maintainable, and efficient API that integrates seamlessly with modern cloud-native architectures.
For teams and developers looking to accelerate application development while maintaining the robustness of Java, Quarkus presents a compelling solution. Organizations can reduce complexity and improve productivity without sacrificing scalability by adopting its developer-friendly approach and efficient runtime.
Opinions expressed by DZone contributors are their own.
Comments