Spring Boot || Tutorial 5 || How to handle PUT and DELETE request using Spring Boot, Update and Delete - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Tuesday, 12 June 2018

Spring Boot || Tutorial 5 || How to handle PUT and DELETE request using Spring Boot, Update and Delete

Hi There, today we will discuss on "How to handle PUT and DELETE request using Spring Boot, Update and Delete". Basically we use PUT request to update a list (or, in db) and DELETE to perform delete element/row in list/DB.

Here we will create an ultra simple project to understand the concept. What all you need to do is follow the bellow steps -

Step-1: Edit the controller class with PUT and DELETE request handler method as explained in below piece of code-

package com.ncteam.thirdtutorial;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReturnObjectFromController {

@Autowired
private ReturnObjectService service ;

@RequestMapping("/listofpojo")
public List<ListPojo> listAllEmployee() {

return  service.getEmployeeList();
}
@RequestMapping("/listofpojo/{id}")
public ListPojo listEmployee(@PathVariable String id) {
return service.getEmployee(id);
}

@RequestMapping(method=RequestMethod.POST, value="/addempl")
public void addEmployee(@RequestBody ListPojo listpojo ) {
service.addEmployee(listpojo);
}
@RequestMapping(method=RequestMethod.PUT, value="/listofpojo/{id}")
public void updateEmployee(@RequestBody ListPojo listpojo, @PathVariable String id ) {
service.updateEmployee(id,listpojo);
}
@RequestMapping(method=RequestMethod.DELETE, value="/listofpojo/{id}")
public void deleteEmployee(@PathVariable String id ) {
service.deleteEmployee(id);
}
}

Step-3: Update service layer class with updateEmployee() and deleteEmployee() method. As explained in below piece of code-

package com.ncteam.thirdtutorial;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class ReturnObjectService {

public List<ListPojo> listEmployee = new ArrayList<>(Arrays.asList( new ListPojo("Emp1","Kennith", "NJ"),
new ListPojo("Emp2","Jorge", "NC"),
new ListPojo("Emp1","Melanie", "India")));
public List<ListPojo> getEmployeeList(){
return listEmployee;
}
public ListPojo getEmployee(String emplId) {
return listEmployee.stream().filter(t->t.getEmplId().equals(emplId)).findFirst().get();
}

public void addEmployee(ListPojo listpojo) {
listEmployee.add(listpojo);
}

public void updateEmployee(String id, ListPojo listpojo) {
for(int i=0;i<listEmployee.size();i++) {
ListPojo list = listEmployee.get(i);
if(list.getEmplId().equals(id)) {
listEmployee.set(i, listpojo);
return;
}
}
}

public void deleteEmployee(String id) {
// TODO Auto-generated method stub
for(int i=0;i<listEmployee.size();i++) {
ListPojo list = listEmployee.get(i);
if(list.getEmplId().equals(id)) {
listEmployee.remove(i);
return;
}
}
}
}

Step-3: We are all good to build the project and test it over postman 

(A) First we will test update process to the list. Below run shows the complete list - we will try to update the info for key/id Emp2. 


(B) Below set up needs to be done to update for Emp2. If you have any question/confusion please let us know in below comment box.


(C) After clicking on the send button if you fetch the complete list you should get the below result - it proves list has been updated.


(D) Now we will delete the key/id Emp1. Below screen has the set up-


(E) After deleting you should be able to get the below list - which clearly shows emp1 has been deleted.


Let us know if you have any question in the below comment box. Happy Reading 😊

No comments:

Post a Comment

Post Bottom Ad