SPRING BOOT @DeleteMapping
SPRING BOOT
@DeleteMapping in Spring Boot is typically used to map HTTP DELETE to delete existing resources from the server by implementing a handler method in a controller.
Spring Boot @DeleteMapping Annotation
➔ @DeleteMapping is a Spring Boot annotation used to map an HTTP DELETE request to a specific handler method in a Spring Boot controller class.
Key features and usage of the @DeleteMapping annotation:
➔ The handler method usually receives the ID from the client and checks if the entity with the ID already exists.
➔ @DeleteMapping deletes the data if it exists, if it does not exist then generates a response code to notify the status.
➔ This annotation performs the standard operation of deleting an existing entity by passing the ID or model to the service layer.
➔ Although @DeleteMapping is commonly used with @PathVariable, the HTTP specification does not prevent a DELETE request from having a request body. When data is received from the client in JSON or XML format, @RequestBody automatically deserializes it into a Java object.
➔ @DeleteMapping is an alternative to @RequestMapping(method = RequestMethod.DELETE), since both annotations do the same thing, either one can be used, but @DeleteMapping is recommended and semantic.
➔ A single method can serve multiple URLs using a string array in the value or path attribute.
Syntax
/* all are equivalent. */
/* both /user/110 and /user/110/remove can do delete operation */
@DeleteMapping({"/user/{id}", "/user/{id}/remove"}) or
@DeleteMapping(value={"/user/{id}", "/user/{id}/remove"}) or
@DeleteMapping(path={"/user/{id}", "/user/{id}/remove"})
➔ The value and path attributes of the @DeleteMapping annotation are aliases for each other and can be used interchangeably.
Syntax
/* All are equivalent. */
@DeleteMapping("/remove/{id}") or
@DeleteMapping(value = "/remove/{id}") or
@DeleteMapping(path = "/remove/{id}")
@DeleteMapping supports attributes to match the requested url
➔ params: Narrow down the mappings based on the request parameters.
Syntax
Example: @DeleteMapping(value = "/delete/{id}", params = "name")
➔ headers: Narrow down the mappings based on the specific header values.
Syntax
Example: @DeleteMapping(value = "/delete/{id}", headers="name=john")
➔ consumes: This specifies the media type to be consumed or received by the controller method (e.g., consumes = "application/json").
➔ produces: This specifies the media type to be produced by the controller method (e.g., produces = "application/json").
Syntax
Example: @DeleteMapping(value = "/delete/{id}", produces="application/xml")
Example: @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
Example: @DeleteMapping(value = "/delete/{id}", produces = {"application/json", "application/xml"})
@DeleteMapping handles dynamic URLs using @PathVariable.
➔ For example, @DeleteMapping("/delete/{id}") can work with handlerName(@PathVariable Long id) to handle dynamic URLs.
Request URL is localhost:8090/delete/101
Syntax
@DeleteMapping(value="/delete/{id}")
public String deleteHandler(@PathVariable Long id) {
return "PathVariable is working fine, ID="+id;
}
@DeleteMapping handles request parameters using @RequestParam.
➔ For example, @DeleteMapping("/delete/{id}") can work with handlerName(@RequestParam(name = "name") String name) to handle query parameters.
➔ Parameters can be sent as a query string in the URL or as form data in the body of the HTTP request.
The request URL is localhost:8090/delete/102?name=john
Syntax
@DeleteMapping(value="/delete/{id}")
public String deleteHandler(@RequestParam("name") String n) {
return "RequestParam is working fine, name="+n;
}
Functionality can be expanded.
➔ @DeleteMapping typically works with IDs but can accept and return anything.
➔ @DeleteMapping can accepts data such as a String, a Java object, or a list of Java objects using the @RequestBody annotation, which Spring Boot automatically converts this incoming data (usually JSON or XML) into the appropriate Java format.
➔ @DeleteMapping can returns data such as a String, a Java object, or a list of Java objects, which Spring Boot automatically converts to the appropriate response format.
Now practice this chapter in your learning environment.
01 Create a Spring Boot project using Spring Tool Suite (STS).
02 Create a Controller class
Name it anything, for example UsersController.
Controller Code
package com.exam.helloworld;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsersController {
@PostMapping("/add/{id}")
public String addHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data + "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
@PutMapping("/update/{id}")
public String updateHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data+ "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
@DeleteMapping("/delete/{id}")
public String deleteIdHandler(@PathVariable Long id) {
return "\nPathVariable is: " + id;
}
}
03 Run HelloworldApplication.java
After successfully run the application.
Now test the @DeleteMapping request url in Postman (an API testing tool).
Send Text data.
Example @RequestParam with @DeleteMappinig
package com.exam.helloworld;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsersController {
@DeleteMapping("/delete/{id}")
public String deleteIdHandler(@PathVariable Long id, @RequestParam String name) {
return "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
}
@RequestParam with @DeleteMapping, output
@DeleteMappinig can receive data using @RequestBody
Example @RequestBody with @DeleteMappinig
package com.exam.helloworld;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsersController {
@PostMapping("/add/{id}")
public String addHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data + "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
@PutMapping("/update/{id}")
public String updateHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data + "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
@DeleteMapping("/delete/{id}")
public String deleteIdHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data + "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
@DeleteMapping("/deletebyname/{name}")
public String deleteNameHandler(@RequestBody String data, @PathVariable Long id, @RequestParam String name) {
return "Data is: " + data + "\nPathVariable is: " + id + "\nRequestParam is: " + name;
}
}
@DeleteMapping with @RequestBody, output