Session 2.4: Spring Boot – REST - CRUD - I
1. Clone the following repository
$ git clone https://github.com/M-Gharib/ESI-W2-CRUD1.git
Note: if you want to create a new Spring Boot project from scratch, you can refer to last week's materials for that. However, you need to install the following dependency:
- Spring Web;
- Spring Data JPA SQL;
- PostgresSQL Driver SQL.
2. Open the application.properties
within the resources folder
src └── main └── java └── resources
3. Add your username, password as well as the name of your database
# Database Settings spring.datasource.url=jdbc:postgresql://localhost:5432/esi2023 # modify if your db is not "esi2023" spring.datasource.username=postgres # modify if your username is not postgres spring.datasource.password=postgres # modify if your password is not postgres # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect # Hibernate ddl auto (create, create-drop, validate, update) # Allows for auto creation of tables spring.jpa.hibernate.ddl-auto = update
4. Check the modification in Product.java
compared to the code in Session 2.1: Spring Boot - Rest API - [Pre Lab]. Note old code is available within this project but it is commented out.
Your teacher will describe most of the code to you, and based on that you should complete the following tasks:
Task 1. write the required code for the updateProduct function in ProductService.java
to update a product in the database
public void updateProduct(String id, Product product) { ..... }
Task 2. write the required code for the updateProduct function in ProductController.java
to invoke the updateProduct function in ProductService.java
@PutMapping("/products/{id}") public void updateProduct(@RequestBody Product product, @PathVariable String id){ ..... }
Task 3. Test whether your code works as expected by sending the related request in RestClientFile.rest
Task 4. write the required code for the deleteProduct function in ProductService.java
to delete a product from the database
public void deleteProduct(String id) { .... }
Task 5. write the required code for the deleteProduct function in ProductController.java
to invoke the deleteProduct function in ProductService.java
@DeleteMapping("/products/{id}") public void deleteProduct(@PathVariable String id){ ..... }
Task 6. Test whether your code works as expected by sending the related request in RestClientFile.rest