- In this project, I have initialized libraries such as Spring Data JPA, Spring Boot DevTools, and the MySQL Driver to manipulate the project.
- After initializing the project, I create packages such as Controller, Dao, Entity, Exception, and Service to contain the corresponding classes. In addition, I also configure the application.properties file to connect to the database.
- In the entity package, I created a class named Employee and used the @Entity annotation to mark this class, and @Table matches the table name in the database. There are also other annotations for the fields.
- Then I created an interface class in the dao package. In this class, I use JPARepository to manipulate add, delete, edit, and read API.
- Then I created an interface class (EmployeeService) and a class (EmployeeServiceImpl). In interface class, I write methods and in class, I create method from interface class. Then I use @Service annotation and declare an interface class (from dao package) to add, remove, edit, and read API methods.
- Then I created an class in the controller package and use @RestController and @RequestMapping annotations. Then declare the interface class (EmployeeService) and instantiate the constructor with arguments, then use the @Autowired annotation for injection. Then I call the method from the interface class that has been handled by the class (EmployeeServiceImpl).
- getEmployees method: This method handles a GET request to "/employee" and retrieves a list of all employees from the employeeService.
- findById method: This method handles a GET request to "/employee/{theId}" and retrieves an employee by their ID from the employeeService. If the employee is not found, it throws a RuntimeException.
- addEmployee method: This method handles a POST request to "/employee" and adds a new employee. The employee object is received in the request body (@RequestBody) and is saved using the employeeService. The saved employee is then returned.
- updateEmploy method: This method handles a PUT request to "/employee" and updates an existing employee. The updated employee object is received in the request body (@RequestBody) and is saved using the employeeService. The saved employee is then returned.
- deleteEmploy method: This method handles a DELETE request to "/employee/{theId}" and deletes an employee by their ID. It first retrieves the employee from the employeeService and throws a RuntimeException if the employee is not found. Then, it calls the delete method on the employeeService to delete the employee. It returns a message indicating the deleted employee ID.
- After testing the functionality, I add the exception handling classes in the exception package.
- The first, I create the StudentErrorResponse class. In this class, I create corresponding fields: status (int), message (String), and timestamp (long).
- The second, I created StudentNotFoundException class and extended with RuntimeException. In this class, I have Genare Constructors from Superclass and initialized three constructors.
- The third, I create a StudentRestExceptionHandler class and use the @RestControllerAdvice annotation. In this class, I have created two methods. One method to handle the message not found and one to handle the input character.
-
Notifications
You must be signed in to change notification settings - Fork 0
Tan-cmd/rest-crud-api
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published