Spring - @DeleteMapping and @PutMapping Annotation
Last Updated :
04 Jan, 2025
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.
Similar Reads
Spring Data JPA - @Id Annotation Spring Data JPA is a important part of Spring Boot applications, providing an abstraction over JPA (Java Persistence API) and simplifying database interactions. JPA is a specification that defines a standard way to interact with relational databases in Java, while Hibernate is one of the most widely
3 min read
Spring Data JPA - @Modifying Annotation @Modifying annotation in Spring Data JPA allows modification queries such as update and delete and enhances the capabilities of the @Query annotation. It enables data transformation actions beyond simple data retrieval, ensuring transaction integrity and improving performance. The @Modifying annotat
4 min read
Spring Data JPA - @Column Annotation In Spring Data JPA, the @Column annotation is used to define column-specific attributes for an entity field in the database. It allows developers to customize column names, set length, define nullability, and more. This is essential when working with relational databases like MySQL, PostgreSQL, and
2 min read
Spring Data JPA - @Table Annotation Spring Data JPA is a powerful framework that simplifies database interactions in Spring Boot applications. The @Table annotation in JPA (Java Persistence API) is used to specify the table name in the database and ensure proper mapping between Java entities and database tables. This is especially use
3 min read
Spring @Lazy Annotation In Spring Framework, the @Lazy annotation can be used to configure the lazy initialization of the beans. By default, Spring initializes all singleton beans eagerly at the application startup. However, in certain scenarios where the bean initialization is resource intensive or not immediately require
3 min read
Difference Between @RequestBody and @ResponseBody Annotation in Spring To achieve the functionality of handling request data and response data in Spring MVC, @RequestBody and @ResponseBody annotations are used. So, in this article, we will go dive into the difference between @RequestBody and @ResponseBody annotations with an example. @RequestBody@RequestBody is mainly
3 min read