欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

@RequestParam和@RequestBody

程序员文章站 2022-04-27 20:27:51
...

一、@RequestParam

GET和POST请求传的参数会自动转换赋值到@RequestParam所注解的变量上

<form action="/requestParamDemo" method="get">
    用户名:<input type="text" name="username"><br>
    用户昵称:<input type="text" name="usernick"><br>
    <input type="submit" value="提交">
</form>
@RequestMapping(value = "/requestParamDemo", method = RequestMethod.GET)
public void requestParamDemo(@RequestParam(value = "username") String userName, @RequestParam(value = "usernick") String userNick) {
	System.out.println("username: " + userName);
	System.out.println("usernick: " + userNick);
}

二、@RequestBody

@RequestBody注解可以接收json格式的数据,并将其转换成对应的数据类型

// 通过ajax发送json数据
{
	"id":1,
	"username":"jack",
	"password":"1234"
}
@RequestMapping(value="/requestBodyDemo", method = RequestMethod.POST)
public void requestBodyDemo(@RequestBody User user){
	System.out.println("id: " + user.getId());
	System.out.println("name: " + user.getName());
	System.out.println("password: " + user.getPassword());
}