This project is a Spring Boot application that demonstrates CRUD (Create, Read, Update, Delete) operations using a PostgreSQL database. The application is designed with best practices for a professional and maintainable codebase, including proper configuration, validation, and exception handling.
- CRUD operations: Create, Read, Update, and Delete operations on a Person entity.
- PostgreSQL integration: Uses PostgreSQL as the database.
- Validation: Validates input data using
@Valid
,@NonNull
and@NotBlank
annotations. - Exception Handling: Global exception handling for clean error responses.
- Java 17 or higher
- Maven 3.6.0 or higher
- PostgreSQL database from Docker
- Getting Started
git clone https://github.com/your-username/spring-boot-crud-postgresql.git
cd spring-boot-crud-postgresql
Create a PostgreSQL database and update the connection settings in src/main/resources/application.properties
.
spring.datasource.url=jdbc:postgresql://localhost:5432/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
docker run --name postgres-db -e POSTGRES_USER=your_username -e POSTGRES_PASSWORD=your_password -e POSTGRES_DB=your_database -p 5432:5432 -d postgres
./mvnw clean install
./mvnw spring-boot:run
-
Get all persons
- GET
/api/v1/person
- GET
-
Get person by ID
- GET
/api/v1/person/{id}
- GET
-
Add new person
- POST
/api/v1/person
- Body:
- POST
{
"name": "John Doe"
}
- Update person
- PUT
/api/v1/person/{id}
- Body:
- PUT
{
"name": "Mohamed Fawzy"
}
- Delete person
- DELETE
/api/v1/person/{id}
- DELETE
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── controller
│ │ │ └── PersonController.java
│ │ ├── model
│ │ │ └── Person.java
│ │ ├── datasource
│ │ │ └── PostgresDataSource.java
│ │ ├── service
│ │ │ └── PersonService.java
│ │ └── dao
│ │ └── PersonDataAccessService.java
│ │ └── PersonDao.java
│ └── resources
│ └── application.properties
│ └── application.yaml
│ └── db
│ └── migration
│ └── V1__PersonTable.sql
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
Contributions are welcome! Please fork the repository and submit a pull request.