Session 3.1: Spring Boot – REST - CRUD - II
Clone the following repository
$ git clone https://github.com/M-Gharib/ESI-W3-CRUD2.git
Note: if you want to create a new Spring Boot project from scratch, you need to install the following dependency:
- Spring Web;
- Spring Data JPA SQL;
- PostgresSQL Driver SQL;
- lombok;
- springdoc.
Your teacher will describe the new structure and code to you.
The application should be working just fine. However, only the code related to the Product
entity has been refactored following the best practices.
Test the application by sending some of the requests in RestClientFile.rest
. Try to add some orders to products, fetch these orders, modify and delete some of them.
Note: modify the request to add an order to a product that already exists in your database.
Testing with @WebMvcTest
ProductControllerTests.java
contains several test cases concerning the ProductController, which can be found in
src └── main └── test/~/esi-w3-crud2 └── ProductControllerTests.java
Your teacher will describe most of these tests work, and based on what you'll learn, you have to write a test case for the PUT
product controller.
Do it yourself: try to write some test cases for the OrderController
relying on what you learned.
Do it yourself: As mentioned earlier, only the code related to the Product
entity has been refactored. Try to refactor the code related to the Order
entity following the same style (e.g., structure the code classes, use dtos, refactor the order controller and service classes).
While refactoring the code, it is advised
- to refactor the services that your API provides one by one, i.e., refactor the GET all request, then, GET by ID, etc.
- to start refactoring the related function in the
@service
, then, refactor the related method handler code in the@RestConttroller
. - to keep a working copy of any code snippet you are refactoring so you can refer to it if something went wrong, i.e., just keep a commented-out version of the code snippet. Then, delete it when everything is working as expected.
- to test your modifications when required by sending the related request in
RestClientFile.rest