Convert Json data by annotation in ssm

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
Insert picture description here
you can see that the incoming Json data is converted to JavaBean type
Insert picture description here

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.
Insert picture description here
Method 2: @RestController
role: the return value of the method in the entire Controller will be converted to the Json data type
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Hambur_/article/details/110877276