You are currently viewing Spring Boot, PostgreSQL, JPA, Hibernate RESTful CRUD API Example

Spring Boot, PostgreSQL, JPA, Hibernate RESTful CRUD API Example


In this tutorial, you will learn how to create a RESTful CRUD API using Spring Boot, PostgreSQL as the database, and JPA with Hibernate for object-relational mapping. We’ll cover each step with detailed code examples, allowing you to build a robust API quickly and efficiently.


Before you begin, ensure you have the following installed:

  • Java Development Kit (JDK)
  • Spring Boot
  • PostgreSQL
  • IDE (Eclipse, IntelliJ IDEA, etc.)

Step 1: Set Up the Project

First, let’s set up a new Spring Boot project.

spring init --name spring-boot-postgresql-crud-api --dependencies web,data-jpa,postgresql

Step 2: Configure PostgreSQL Database

Next, configure PostgreSQL as the database. Open and add:


Replace db_name, your_username, and your_password with your PostgreSQL database details.

Step 3: Create Entity Class

Create an entity class representing the data model. For example, let’s create a Product entity.

import javax.persistence.*;

@Table(name = "products")
public class Product {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private double price;

    // Getters and setters

Step 4: Create Repository Interface

Create a repository interface for the entity to perform CRUD operations.


public interface ProductRepository extends JpaRepository<Product, Long> {

Step 5: Create REST Controller

Now, create a REST controller to handle HTTP requests.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class ProductController {

    private ProductRepository productRepository;

    public List<Product> getAllProducts() {
        return productRepository.findAll();

    public Product createProduct(@RequestBody Product product) {

    public Product getProductById(@PathVariable Long id) {
        return productRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Product", "id", id));

    public Product updateProduct(@PathVariable Long id, @RequestBody Product productDetails) {
        Product product = productRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Product", "id", id));



    public ResponseEntity<?> deleteProduct(@PathVariable Long id) {
        Product product = productRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Product", "id", id));


        return ResponseEntity.ok().build();

Step 6: Run the Application

Finally, run the Spring Boot application and test the CRUD API endpoints using tools like Postman or cURL.

mvn spring-boot:run

That’s it! You’ve successfully built a RESTful CRUD API using Spring Boot, PostgreSQL, JPA, and Hibernate.

Feel free to expand upon this example by adding validation, error handling, security, or additional endpoints as needed. Happy coding!

Leave a Reply