首页 > 其他分享 >67-@RequestParam、@PathVariable、@RequestBody、@ModelAttribute的用法

67-@RequestParam、@PathVariable、@RequestBody、@ModelAttribute的用法

时间:2022-09-29 11:37:50浏览次数:59  
标签:PathVariable RequestParam public RequestBody sysUserService getById 111 SysUser 

@RestController
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/getUser/{id}")
    public SysUser getUser(@PathVariable("id")String id){
        System.out.println(id);
        SysUser u = sysUserService.getById(id);
        u.setA1("111");
        return u;
    }

    @PostMapping("/getUser1")
    public SysUser getUser1(@RequestParam("id")String id){
        System.out.println(id);
        SysUser u = sysUserService.getById(id);
        u.setA1("111");
        return u;
    }

    @PostMapping("/getUser2")
    public SysUser getUser2(@RequestBody String str){
        System.out.println(str);
        SysUser model = JSON.parseObject(str, SysUser.class);
        SysUser u = sysUserService.getById(model.getUserId());
        u.setA1("111");
        return u;
    }
    @PostMapping("/getUser3")
    public SysUser getUser3(@ModelAttribute("ma") SysUser ma){
        SysUser u = sysUserService.getById(ma.getUserId());
        u.setA1("111");
        return u;
    }


}

PathVariable的请求: image

RequestParam的请求: image

RequestBody的请求:

image

ModelAttribute的请求:

image

标签:PathVariable,RequestParam,public,RequestBody,sysUserService,getById,111,SysUser,
From: https://blog.51cto.com/u_14816966/5722072

相关文章