View video tutorial

SPRING BOOT @PutMapping

SPRING BOOT

The @PutMapping annotation is typically used to update existing resources on the server using the HTTP PUT method.

Spring Boot @PutMapping annotation


➔ @PutMapping is a Spring Boot annotation used to map an HTTP PUT request to a specific handler method in a Spring Boot controller class.

Key characteristics and usage of @PutMapping annotation:


➔ The handler method receives data from the client and checks whether the entity or data already exists.

➔ @PutMapping can be used by implementations to create a resource, if it does not already exist.

➔ This annotation performs the standard operation of updating existing entities and passing this updated entity or model to the service layer.

➔ When data is received from the client in JSON or XML format, @RequestBody automatically deserializes it into a Java object.

➔ @PutMapping is an alternative to @RequestMapping(method = RequestMethod.POST) because both annotations perform the same function, either of them can be used, but @PutMapping is recommended.

➔ A single method can serve multiple URLs using a string array in the value or path attribute, for example, @PutMapping(path={"/update","/edit"}).

Syntax
/* all are equivalent. */
@PutMapping({"/update", "/edit"}) or 
@PutMapping(value={"/update", "/edit"}) or 
@PutMapping(path={"/update", "/edit"})

➔ The value and path attributes of the @PutMapping annotation are aliases for each other and can be used interchangeably.

Syntax
/* All are equivalent. */
@PutMapping("/edit") or 
@PutMapping(value = "/edit") or 
@PutMapping(path = "/edit") 

@PutMapping supports attributes to match the requested url


➔ params: Narrowing mappings based on the request parameters.

Syntax
Example: @PutMapping(value = "/update", params = "name")

➔ headers: Narrowing mappings based on the specific header values.

Syntax
 Example: @PutMapping(value = "/update", headers="name=john")

➔ consumes: This specifies that the media type can be consumed or received by the controller method (e.g., consumes = "application/json").

➔ produces: This specifies that the media type should be produced by the controller method (e.g., produces = "application/json").

Syntax
Example: @PutMapping(value = "/update", produces="application/xml")
Example: @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
Example: @PutMapping(value = "/update", produces = {"application/json", "application/xml"})

@PutMapping handles dynamic URLs using @PathVariable.


➔ For example, @PutMapping("/update/{id}") can work with handlerName(@PathVariable Long id) to handle dynamic URLs.

Request URL is localhost:8090/update/101

Syntax
@PutMapping(value="/update/{id}")
public String updateHandler(@PathVariable Long id) {
	return "PathVariable is working fine, ID="+id;
}


@PutMapping handles request parameters using @RequestParam.


➔ For example, @PutMapping(/update) 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/update?name=john

Syntax
@PutMapping(value="/update")
public String updateHandler(@RequestParam("name") String n) {
	return "RequestParam is working fine, name="+n;
}


Common usage and best practices:


➔ @PutMapping typically 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.

➔ @PutMapping typically 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.

02 Create a Controller class

Create controller

Name it anything, for example UsersController.

Create UsersController

Controller Code
package com.exam.helloworld;

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")
	public String updateHandler(@RequestBody String data) {
		return "Data is: " + data;
	}
}
                    

03 Run HelloworldApplication.java

spring boot application run

After successfully run the application.

restapi server run

Now test the @PutMapping request url in Postman (an API testing tool).

Send JSON object.

PutMapping JSON

Send JavaScript object.

PutMapping JavaScript Object

JSON and JavaScript object data types.


JSON Data types:

In JSON, keys must be enclosed in double quotes, and single quotes are not allowed.

Values ​​can be enclosed in double quotes or without any quotes, depending on their data type and content. for example {"name": "Maria", "age": 22}. Single quotes are not allowed.

➔ String: The value must be enclosed in double quotes for example "John Doe".

➔ Number: An integer or floating-point number.

➔ Boolean: true or false.

➔ null: An empty value (null).

➔ Object: A collection of key-value pairs enclosed in curly braces {} and the Keys must be strings.

➔ Array: A list of values enclosed in square brackets [].


JavaScript Object Data Types:

The key can be enclosed in single quotes, double quotes or identifire without qoute. for example 'name', "name", name.

Values ​​can be enclosed in double quotes, single quotes, or without any quotes, depending on their data type and content.

➔ All json type such as Strings, numbers, booleans, null, objects, and arrays.

➔ undefined: A variable has been declared but its value has not been assigned.

➔ BigInt: Beyond the standard number limit.

➔ Function: A method within the object.

➔ Built-in object types: Such as Date, Map, Set, RegExp, etc.

Send Text data.

PutMapping  text

Example @PathVariable
package com.exam.helloworld;

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 {

	@PutMapping("/update/{id}")
	public String updateHandler(@RequestBody String data, @PathVariable Long id) {
		return "Data is: " + data+ "\nPathVariable is: " + id ;
	}
}
                    


@PathVariable with @PutMapping, output
PathVariable PutMapping output

Example @RequestParam
package com.exam.helloworld;

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 {

	@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;
	}
}


@RequestParam with @PutMapping, output
RequestParam PutMapping output