Lab2_Solution

.pdf

School

University of Toronto *

*We aren’t endorsed by this school

Course

350

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

6

Uploaded by MagistrateWorldChinchilla30

Report
MIE 350 Lab 2 Answers Question 1: 1. Open StudentController.java (the REST controller for the Student entities). a. Locate the function retrieveAllStudents() i. The function is annotated with @GetMapping that indicates a GET request handler accessed through the URL “/students”. What is the CRUD operation that is normally associated with a GET request? Read. A GET request usually performs Read operation on the contents of the database/repository. ii. What do you think the purpose of this function? This function retrieves information of all students in our student Table. iii. Use the Insomnia client to send a GET request to http://localhost:8085/students as in the following image. Press SEND. What did you get as a result for your query (in the right side under “Preview”) GET: http://localhost:8085/students In the preview we see a list of all students in the student table along with their attributes, including the list of marks for each student. Within the list of marks, each mark element includes the composite key (composed of student id and course id), the student entity, the course entity and the mark value. b. Next, locate the function createStudent(@RequestBody Student newStudent) i. What type of HTTP requests is this function handling? What CRUD operation is associated with this type of HTTP requests? Create. ii. What do you expect the function to do? This function can be used to add a new student to the database.
iii. Use the Insomnia client and send a request to create a student. Note: as indicated by the function’s argument, the body of the request needs to be a JSON representation of the Student entity. Following is an example JSON for creating a student in the CMS and an image on how to send such request using the Insomnia client. { "id": 9999, "firstName": "myFirstName", "lastName": "myLastName", "email": "my@email.ca" } Follow instructions provided in the question. You can modify firstname, lastName and email values to your choice. POST: http://localhost:8085/students With JSON body iv. Run a similar GET request as in 1(a). Did the result change? GET: http://localhost:8085/students Ensure the body of the request is of "No BODY" type (since it is a GET request). Yes the results show the new student added at the end of the preview list. c. Examine the function @GetMapping("/students/{id}") retrieveStudent(@PathVariable("id") Long studentId) i. The function retrieves the details of a single student based on their student ID provided as part of the URL. Note: as indicated by the function’s argument, the URL variable id will be mapped to the argument studentId of type Long. For example, sending a GET request to /students/5555 will run this function and map the value 5555 to the studentId argument. The @PathVariable annotation is used to extract the value of the template variable “id” (mentioned in the URL of the request), and assign their value to a method variable studentId of Long type. ii. Use the Insomnia client and retrieve the student details for the student with the student ID 5555. What do you see?
GET: http://localhost:8085/students/5555 Retrieves all information about the student with id 5555: { "id": 5555, "firstName": "Jon", "lastName": "Snow", "email": "jon.snow@mail.univ.ca", "marks": [ { "markId": { "studentId": 5555, …. d. Examine the rest of the code in StudentController.java: i. Locate the function that is responsible for updating the details of a student and identify the type of HTTP request, the URL, and the parameters it takes. Then, use the Insomnia client to update the name and email of one of the existing students. updateStudent is the function that is used to update student. PutMapping HTTP request initiates the update of the student entity. It expects a Student entity/object within the body of the request and the student id specified in the URL of the request. The URL used to call this function is: PUT: http://localhost:8085/students/5555 JSON: { "Id": 5555, "firstName": "Jonathan", "lastName": "Smith", "email": "jon.smith@mail.univ.ca" } Note: We change the firstName, lastName and email in the request. ii. Retrieve the student details of the student you just updated and see the student’s information. Did both the name and email of the student update? If not, what field has not been updated?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help