Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaSpring – @DeleteMapping and @PutMapping Annotation

Spring – @DeleteMapping and @PutMapping Annotation

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.

What is meant by @DeleteMapping annotation?

@DeleteMapping annotation in Spring MVC is a powerful tool for handling HTTP DELETE requests in your RESTful web services. It maps a specific URLs handler method allowing you to receive and process the data submitted through DELETE requests. The @DeleteMapping annotation is a Spring annotation that is used to map HTTP DELETE requests onto specific handler methods. It is a shortcut for @RequestMapping annotation with method = RequestMethod.DELETE attribute. 

Examples of @DeleteMapping annotation

Example 1: 

Java




// on the below line we are adding @DeleteMapping annotation
// and passing end point to it. 
 @DeleteMapping("/users/{id}")
 public void deleteUser(@PathVariable("id") String id) {
        // Delete the user in this method with the id. 
 }


In the above example, the deleteUser method is annotated with @DeleteMapping annotation. This means that the deleteUser method will be called when HTTP Delete Request is received at the /users/{id} URL. Inside the deleteUser method, we are passing a parameter for Request Param to be sent while making a Delete request. In the request param, we are passing the id for the user which we have to delete while making a delete request. 

Example 2: 

Java




// on the below line we are adding @DeleteMapping 
// annotation and passing end point to it. 
 @DeleteMapping("/{id}")
 public String deleteFile(@PathVariable("id") String id) {
        // inside this method we have to write a logic to delete the file.
        // on the below line returning message as file deleted succesfully. 
        return "File deleted ";
 }


In the above example, we are creating a deleteFile method which is annoyed with the @DeleteMapping annotation. This means that the deleteFile method will be called when an HTTP Delete request is received at the /{id} URL. Inside this deleteFile method, we are also passing a parameter as id for the file which we have to delete. Inside the deleteFile method we have to write the logic for the response received and we are returning the message as File Deleted message.  

What is meant by @PutMapping annotation?

@PutMapping annotation in String MVC framework is a powerful tool for handling HTTP PUT requests in your RESTful web services. It maps specific URLs to the handler method allowing you to receive and process the data submitted through PUT requests. The @PutMaping annotation is a Spring annotation that is used to map HTTP PUT requests onto specific handler methods. It is a shortcut for @RequestMapping annotation with method = RequestMethod.PUT attribute. 

Examples of @PutMapping annotation

Example 1: 

Java




// on the below line we are adding @PutMapping 
// annotation and passing end point to it. 
@PutMapping("/users")
public void updateUser(@PathVariable("id") String id, @RequestParam("userName") String userName) {
    // inside this method we have to update the user record
}


In the above example, the updateUser method is annotated with @PutMapping annotation. This means that the updateUser method will be called when HTTP Put Request is received at the /users URL. Inside the updateUser method, we are also passing a parameter for Request Param to be sent while making a put request. In the request param, we are passing the user name which we have to update while making a put request.  

Example 2: 

Java




// on the below line we are adding @PutMapping
// annotation and passing end point to it. 
@PutMapping("/users/{id}")
public void updateUser(@PathVariable("id") String id, @RequestBody User user) {
        // Update the user details here
}


In the above example, the updateUser method is annotated with @PutMapping annotation. This means that the updateUser method will be called when the HTTP Put request is received at the /users/{id} URL. Inside this updateUser method, we are also passing a parameter for Request Body to be sent while making a PUT request. In the request body, we have to pass the id for which we have to update the user details and the object for the student which will contain the updated values of the student.

RELATED ARTICLES

Most Popular

Recent Comments