Skip to content

reiandfrey/springboot-core-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Spring Boot Core REST API Implementation

GitHub Release Java Spring Boot

Overview

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.

Features

  • 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.

Technologies Used

  • 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.

Getting Started

To get started with this project, follow these steps:

  1. Clone the Repository:

    git clone https://github.com/reiandfrey/springboot-core-rest-api.git
  2. Navigate to the Project Directory:

    cd springboot-core-rest-api
  3. Build the Project: Use Maven to build the project.

    mvn clean install
  4. Run the Application: After building, you can run the application using:

    mvn spring-boot:run
  5. Access the API: Open your browser and navigate to http://localhost:8080/api.

API Endpoints

The following are the main API endpoints available in this project:

User Management

  • 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.

Product Management

  • 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.

Database Configuration

H2 Database

For testing purposes, H2 is configured as an in-memory database. The configuration is located in src/main/resources/application.properties.

PostgreSQL Configuration

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>

Running Tests

To run the tests included in this project, execute the following command:

mvn test

Deployment

This application can be deployed on AWS Lightsail. Follow these steps:

  1. Create a Lightsail Instance: Use the AWS console to create a new Lightsail instance.
  2. Install Java and Maven: SSH into your instance and install Java and Maven.
  3. Clone the Repository: Clone the repository to your instance.
  4. Build and Run: Follow the build and run steps mentioned above.

Contributing

Contributions are welcome! If you would like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Releases

You can find the latest releases here. Download the necessary files and execute them as required.

Contact

For any questions or feedback, feel free to reach out via GitHub issues or directly through my profile.

Acknowledgments

  • Thanks to the Spring Boot community for their ongoing support and resources.
  • Special thanks to the contributors who help improve this project.

Additional Resources


Spring Boot


API 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 Strategies

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.

Monitoring and Logging

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.

Sample Logback Configuration

<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

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>

Basic Authentication Example

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();
    }
}

Best Practices

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.

Conclusion

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.