Skip to content

Spring Boot Rest API With Exception Handling, Response Handling, and Unit Testing

Notifications You must be signed in to change notification settings

Paras2322/SpringBoot-RestAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Rest API With Exception Handling, Response Handling, and Unit Testing With JUnit5

Introduction

This project demonstrates building a RESTful API using Spring Boot with features like exception handling, response handling, and unit testing. It includes integration with a MySQL database using Spring Data JPA, and utilizes technologies such as Mockito, AssertJ, JUnit 5, and H2 database for testing purposes.

Technologies Used

  • Java
  • Spring Boot
  • Mockito
  • AssertJ
  • MySQL Database
  • JUnit 5
  • H2 Database
  • Exception Handling
  • Response Handling
  • Spring Data JPA

Project Structure

The project is structured as follows:

  • Main Application Class: RestApiApplication.java
  • Controller: Contains endpoints for handling HTTP requests (EmployeeController.java)
  • Service Layer: Contains business logic (EmployeeService.java, EmployeeServiceImpl.java)
  • Repository Layer: Provides access to the database using Spring Data JPA (EmployeeRepository.java)
  • Model: Contains data models (Employee.java)
  • Exception Handling: Custom exceptions and exception handler (EmployeeNotFoundException.java, NoEmployeesFoundException.java, EmployeeExceptionHandler.java)
  • Response Handling: Response handler for consistent API responses (ResponseHandler.java)
  • Unit Tests: Test cases for controllers, services, and exception handling using Mockito, AssertJ, and JUnit 5

Running the Application

  1. Clone the repository.
  2. Configure the MySQL database connection in application.yaml.
  3. Run the application using Maven or an IDE.
  4. Use tools like Postman or cURL to make API requests.

Running Tests

  1. Unit tests can be run using the Maven command: mvn test.

Resources

Contributions

Contributions to this repository are welcome! If you have any examples, tutorials, improvements, or additional features related to REST API development, we'd love to see them. Whether it's enhancing error handling, optimizing performance, adding new endpoints, or anything else that can benefit the project, feel free to submit a pull request.

Guidelines for Contributions:

  • Before contributing, please ensure that your code follows the existing coding style and conventions.
  • If you're adding new functionality, consider including relevant unit tests to maintain code quality.
  • Provide clear and concise documentation for any changes or new features you introduce.
  • Be respectful and considerate of other contributors. We value collaboration and open communication.

How to Contribute:

  1. Fork the repository to your own GitHub account.
  2. Clone the forked repository to your local machine.
  3. Create a new branch for your changes: git checkout -b feature/new-feature.
  4. Implement your changes and ensure they pass all tests.
  5. Commit your changes with descriptive commit messages: git commit -am 'Add new feature: Feature Description'.
  6. Push your changes to your fork: git push origin feature/new-feature.
  7. Submit a pull request to the main repository.

Your contributions can help improve this project and make it more valuable to the community. Thank you for your interest and support!

Releases

No releases published

Packages

No packages published

Languages