In the Controller class:
1.@RequestBody
function: convert the data obtained by the front end into JavaBean form
//@RequestBody : 此时传入的 参数 user对象 为Json数据类型
@RequestMapping("/user/modify.action")
public User modifyUser(@RequestBody User user) {
System.out.println("user is " + user);
final int ret = service.modifyUser(user);
final User userDb = service.findUser(user.getId());
return userDb;
}
Pass a Json data to the Controller and
you can see that the incoming Json data is converted to JavaBean type
2.
Method 1: @ResponseBody
function: convert the data into Json and send it to the front end
//@ResponseBody : 就是将 方法的返回值 转换 成 json串 。
@ResponseBody
@RequestMapping("/user.action")
public User getOneUser(Integer id) {
System.out.println("id is " + id);
final User user = service.findUser(id);
return user;
}
It will return a user in the form of Json data.
Method 2: @RestController
role: the return value of the method in the entire Controller will be converted to the Json data type