Spring Restful WS – HTTP Delete Request

Spread the love

In this tutorial, we will see how to send a delete request and handle a delete request at the server side.

Client sends a Delete request when it wants to delete something on an existing set of data or resource which is already present on the server.

Consider the below request sent by the client through POTSMAN UI to delete employee details.

Consider the above DELETE request. We are sending only an Emp Id in the url (100) for which we want to delete the details.

Let’s implement the Sever Side DELETE method handler in the REST controller.

Class EmployeeReqDetails
{
   private String name;
   private String age;
   private String designation;

.... public getters and setters
}

Class Employee
{
   private String empId;
   private String name;
   private String age;
   private String designation;

....public getters and setters

}

com.techfinanceworld.restapp.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("employees")
public Class EmpController{

Map<String, Employee> emps;

   @PostMapping(produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE}) //line 4
   public ResponseEntity<Employee> CreateEmployee(@RequestBody EmployeeReqDetails erd) //line 5
   {
      Employee e = new Employee();
      e.setName(erd.getName());
      e.setAge(erd.getAge());
      e.setDesignation(erd.getDesignation());

      String empId = Math.random().toString();
      e.setEmpId(empId);

      if (emps = null){
        emps = new HashMap<>();
      }

      emps.put(empId,emp);
      return new ResponseEntity<Employee>(e,HttpStatus.OK);
 }

   @PutMapping(path = "/{empId}", produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE}) //line 4
   public Employee CreateEmployee(@PathVariable String empId, @RequestBody EmployeeReqDetails erd) //line 5
   {
      Employee e = emps.get(empId);
      e.setName(erd.getName());
      e.setAge(erd.getAge());
      e.setDesignation(erd.getDesignation());

      emps.put(empId,e);
      
      return e;
   }

@DeleteMapping(path = "/{empId}") 
   public ResponseEntity CreateEmployee(@PathVariable String empId)
   {
       emps.remove(empId);
       return ResponseEntity<>(HttpStatus.OK);
       
   }

}

As seen above, using POST request we are creating/adding the employees in the map and using PUT we are updating the employee details in the map.

Using Delete request, we are deleting employee details from the map and just returning an Http response code as 200 which is OK.

Annotation used is @DeleteMapping with the method which handles the Delete Request.

One Reply to “Spring Restful WS – HTTP Delete Request”

  1. Hey,

    It was nice speaking to you the other day, this is the service I was telling you about that helped us boost our ROI almost 2000%

    Its a company called Lifemail.studio sorry it took so long to get back to you. They allow you to send any email doesn’t matter what.

    We dealt with a guy named Michael, he was friendly and got us setup really quickly.

    Regards,
    Aisha

Comments are closed.