@PutMapping Spring Boot Example



Spring @PutMapping example shows how to use @PutMapping annotation to map HTTP PUT requests onto specific handler methods.
Check out free Spring boot projects at https://www.sourcecodeexamples.net/p/free-spring-boot-projects.html 

Before understanding about @PutMapping annotation, let's first understand what is PUT HTTP method? 

What is PUT?

PUT HTTP method is used to modify/update a resource where the client sends data that updates the entire resource.

It is used to set an entity’s information completely. PUT is similar to POST in that it can create resources, but it does so when there is a defined URI. PUT overwrites the entire entity if it already exists, and creates a new resource if it doesn’t exist.

@PutMapping Annotation Overview

As we know PUT HTTP method is used to update/modify the resource so @PutMapping annotation is used for mapping HTTP PUT requests onto specific handler methods.
Specifically, @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT).
@PutMapping annotation has below optional attributes:
consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
headers  – The headers of the mapped request, narrowing the primary mapping.
name – Assign a name to this mapping.
params – The parameters of the mapped request, narrowing the primary mapping.
path – The primary mapping expressed by this annotation.
produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
value – The primary mapping expressed by this annotation.

@PutMapping Example

Consider we want to update the existing Employee resource so the below example demonstrates the usage of @PutMapping annotation, which is used to map "/employees/{id}" HTTP PUT request onto a specific handler method - updateEmployee:
@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long employeeId,
  @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
     Employee employee = employeeRepository.findById(employeeId)
     .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));

     employee.setEmailId(employeeDetails.getEmailId());
     employee.setLastName(employeeDetails.getLastName());
     employee.setFirstName(employeeDetails.getFirstName());
     final Employee updatedEmployee = employeeRepository.save(employee);
     return ResponseEntity.ok(updatedEmployee);
}
Here is one more example to update user PUT HTTP request using @PutMapping annotation:
    @PutMapping("/users/{id}")
    public ResponseEntity<User> updateUser(
    @PathVariable(value = "id") Long userId,
    @Valid @RequestBody User userDetails) throws ResourceNotFoundException {
         User user = userRepository.findById(userId)
          .orElseThrow(() -> new ResourceNotFoundException("User not found on :: "+ userId));
  
        user.setEmailId(userDetails.getEmailId());
        user.setLastName(userDetails.getLastName());
        user.setFirstName(userDetails.getFirstName());
        user.setUpdatedAt(new Date());
        final User updatedUser = userRepository.save(user);
        return ResponseEntity.ok(updatedUser);
   }

Check out a complete example at Spring Boot 2 Hibernate 5 MySQL CRUD REST API Tutorial

Reference

Comments