@PutMapping Example in Spring MVC

In this Spring MVC tutorial, you will learn how to use @PutMapping annotation in your RESTful Web Services application to be able to accept HTTP Put request containing a Request Body with JSON or XML payload, so that you can perform let’s say an update user details operation.

So, let’s say you want to send HTTP PUT request with JSON containing user details you want to update. Below is an example of CURL command which sends such HTTP PUT Request.

HTTP PUT Request with CURL command

The below HTTP PUT Request will be sent to a below URL:

http://localhost:8080/api/users/2d8d4927572642cebccdf5257880a393

where the “2d8d4927572642cebccdf5257880a393” is the ID of the user the details of which would like to update.

and the

{
  "lastName":"Kargopolov",
  "firstName":"Sergey"
}

is the JSON payload which contains the new user details.

Complete CURL command: 

curl -X PUT \
  http://localhost:8080/api/users/2d8d4927572642cebccdf5257880a393 \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
  "lastName":"Kargopolov",
  "firstName":"Sergey"
}'

@PutMapping Annotation to Handle HTTP PUT Request

Now let’s create a @RestController class with a method that will handle the above HTTP PUT Request.

Reading Path Variable 

To map one of your methods to an HTTP PUT Request you need to simply annotate it with a @PutMapping annotation. But because the above HTTP PUT request contains a Path Parameter Variable /2d8d4927572642cebccdf5257880a393, we will need to annotate our method with @PutMapping(“/{userId}”) instead.

To read the userId variable defined in @PutMapping(“/{userId}”), annotate one of the method arguments “String userId” with the @PathVariable. Just like it is in the example below.

updateUser(@PathVariable String userId)

Reading JSON Request Body

To read the JSON Request Body from HTTP Put request, annotate another method argument with @RequestBody annotation. The method argument annotated with @RequestBody should be a class into which the JSON request body will be mapped and converted. Below I will show you the source code of the UserDetailsRequestModel class which I have annotated with the @RequestBody. 

Ok, here is the final example:

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    UserService userService;
 
    @PutMapping("/{userId}", 
    consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, 
    produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} )
    public UserRest updateUser(@PathVariable String userId, @RequestBody UserDetailsRequestModel requestUserDetails)    {
        UserRest returnValue = new UserRest();

        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(requestUserDetails, userDto);

        UserDto updatedUserDetails = userService.updateUser(userDto, userId);
        BeanUtils.copyProperties(updatedUserDetails, returnValue);

        return returnValue;
    }

}

The UserDetailsRequestModel class

public class UserDetailsRequestModel {
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
    
}

@PutMapping – Video Tutorial

I hope this short Spring MVC tutorial on how to use @PutMapping to handle HTTP PUT request in your RESTful Web Services app was helpful.

Have a look at the below list of video courses, will you find one of them helpful?