This repository contains a core Spring Boot REST API implementation designed with clean architecture principles. It serves as a demonstration of fundamental web service patterns and best practices for enterprise development. The project integrates various technologies and frameworks, making it a solid foundation for building scalable and maintainable applications.
- Clean Architecture: The project follows clean architecture principles, ensuring separation of concerns and enhancing testability.
- RESTful API: Built using Spring Boot, it provides a robust RESTful API for handling HTTP requests.
- Database Support: Utilizes H2 for in-memory database testing and PostgreSQL for production-level persistence.
- ORM with Hibernate: Employs Hibernate for object-relational mapping, simplifying database interactions.
- Deployment on AWS: Designed to be easily deployable on AWS services like Lightsail.
- Maven Build: Uses Maven for project management and dependency resolution.
- Java: The primary programming language.
- Spring Boot: Framework for building the REST API.
- H2 Database: In-memory database for testing.
- PostgreSQL: Production-level database.
- Hibernate: ORM framework for database operations.
- AWS Lightsail: For deployment.
- JPA: Java Persistence API for managing relational data.
- Maven: Build automation tool.
To get started with this project, follow these steps:
-
Clone the Repository:
git clone https://github.com/reiandfrey/springboot-core-rest-api.git
-
Navigate to the Project Directory:
cd springboot-core-rest-api
-
Build the Project: Use Maven to build the project.
mvn clean install
-
Run the Application: After building, you can run the application using:
mvn spring-boot:run
-
Access the API: Open your browser and navigate to
http://localhost:8080/api
.
The following are the main API endpoints available in this project:
- GET /api/users: Retrieve a list of all users.
- POST /api/users: Create a new user.
- GET /api/users/{id}: Retrieve a user by ID.
- PUT /api/users/{id}: Update an existing user.
- DELETE /api/users/{id}: Delete a user by ID.
- GET /api/products: Retrieve a list of all products.
- POST /api/products: Create a new product.
- GET /api/products/{id}: Retrieve a product by ID.
- PUT /api/products/{id}: Update an existing product.
- DELETE /api/products/{id}: Delete a product by ID.
For testing purposes, H2 is configured as an in-memory database. The configuration is located in src/main/resources/application.properties
.
For production, you can configure PostgreSQL in the same application.properties
file. Update the following properties:
spring.datasource.url=jdbc:postgresql://<host>:<port>/<database>
spring.datasource.username=<username>
spring.datasource.password=<password>
To run the tests included in this project, execute the following command:
mvn test
This application can be deployed on AWS Lightsail. Follow these steps:
- Create a Lightsail Instance: Use the AWS console to create a new Lightsail instance.
- Install Java and Maven: SSH into your instance and install Java and Maven.
- Clone the Repository: Clone the repository to your instance.
- Build and Run: Follow the build and run steps mentioned above.
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes and commit them (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
You can find the latest releases here. Download the necessary files and execute them as required.
For any questions or feedback, feel free to reach out via GitHub issues or directly through my profile.
- Thanks to the Spring Boot community for their ongoing support and resources.
- Special thanks to the contributors who help improve this project.
- Spring Boot Documentation
- Hibernate Documentation
- PostgreSQL Documentation
- AWS Lightsail Documentation
For detailed API documentation, you can use tools like Swagger. Integrate Swagger UI into your project by adding the following dependency to your pom.xml
:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
After adding the dependency, you can access the Swagger UI at http://localhost:8080/swagger-ui/
.
Testing is crucial for maintaining the quality of your application. This project includes unit tests and integration tests. Here are some strategies to follow:
- Unit Tests: Test individual components and services.
- Integration Tests: Test the interaction between components.
- End-to-End Tests: Simulate user interactions and verify the complete flow of the application.
Use JUnit and Mockito for writing tests. You can add test classes in the src/test/java
directory.
For production applications, monitoring and logging are essential. Integrate tools like:
- Spring Actuator: Provides endpoints for monitoring the application.
- Logback: For logging purposes. Configure it in
src/main/resources/logback.xml
.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Security is vital for any web application. Consider implementing:
- Spring Security: To handle authentication and authorization.
- HTTPS: Ensure secure communication between the client and server.
You can start by adding the Spring Security dependency to your pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
You can configure basic authentication in your SecurityConfig
class:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
To maintain a high-quality codebase, follow these best practices:
- Code Reviews: Regularly review code to catch issues early.
- Documentation: Keep documentation up to date for easier onboarding.
- Version Control: Use branches effectively in Git to manage features and fixes.
This README provides a comprehensive guide to the Spring Boot Core REST API project. Follow the steps to set up your environment, run the application, and explore its features. For further inquiries, refer to the contact section or open an issue on GitHub.