- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 381字
- 2021-07-02 19:38:20
The RequestBody annotation
In this section, we will learn when and how to use the RequestBody annotation (@RequestBody) for handling web requests.
The RequestBody annotation is used to bind the method parameter with the body of the incoming request. In the process of binding, HttpMessageConverters converts the body of the request appropriately (most commonly into the parameter object) based on the content type of the request.
The RequestBody annotation is most commonly used in scenarios dealing with REST APIs.
The following example demonstrates the usage of the @RequestBody annotation using a domain object, User, which is made a parameter to the controller method:
@RestController
public class RestDemoController {
@PostMapping("/hello")
public HelloMessage getHelloMessage(@RequestBody User user) {
HelloMessage helloMessage = new HelloMessage();
String name = user.getName();
helloMessage.setMessage( "Hello " + name + "! How are you doing?");
helloMessage.setName(name);
return helloMessage;
}
}
In the preceding example, note some of the following:
- The @PostMapping annotation maps the REST API endpoint, /hello, with the handler method, getHelloMessage. Recall that @PostMapping is a composed annotation which acts as a shortcut for @RequestMapping (method = RequestMethod.POST).
- The @RequestBody annotation is used with the User object. This binds (or maps) the method parameter, user of type User, with the body of the web request. The body of the request arrives in the following JSON format:
{"name": "Calvin Hobbes"}
The HttpMessageConverter method converts the preceding into the User object, whose code looks like the following:
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- The @RestController annotation, a convenient annotation, which is itself annotated with @Controller and @ResponseBody annotations, and is used for programming REST API integration endpoints.
- The HelloMessage class is returned as a response. The following is the code for HelloMessage:
public class HelloMessage {
private String message;
private String name;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The HttpMessageConverter method converts the preceding response object into the following response message:
{"message": "message text goes here...", "name": "name goes here..."}
- DBA攻堅指南:左手Oracle,右手MySQL
- Objective-C應(yīng)用開發(fā)全程實(shí)錄
- Learning Chef
- Internet of Things with Intel Galileo
- JavaScript 程序設(shè)計案例教程
- Apache Spark 2.x for Java Developers
- Python Data Science Cookbook
- SQL Server 2008中文版項目教程(第3版)
- 從程序員角度學(xué)習(xí)數(shù)據(jù)庫技術(shù)(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- Unity 5.X從入門到精通
- PhoneGap 4 Mobile Application Development Cookbook
- jQuery從入門到精通(微課精編版)
- Hands-On Dependency Injection in Go
- 數(shù)字媒體技術(shù)概論
- 零基礎(chǔ)學(xué)編程系列(全5冊)