1.不加任何注解的直接映射(只适用GET请求)
@RequestMapping("/addUser")
public String addUser1(String username,String password) {}
url 请求格式:http://localhost/addUser?username=xxx&password=111111
注意:请求参数名必须与接口参数名对应
2.接收restful风格参数
@RequestMapping(value="/addUser/{username}/{password}")
public String addUser(@PathVariable String username,@PathVariable String password) {}
url 请求格式:http://localhost/addUser/xxx/111111
3.用注解@RequestParam绑定请求参数到方法入参
@RequestMapping(value="/addUser")
public String addUser(@RequestParam("username") String username,@RequestParam("password") String password) {}
4.使用@RequestBody 封装成对象(只适用POST请求)
@RequestMapping("/addUser")
public String addUser(@RequestBody User user) {}
5.使用@ModelAttribute 封装成对象
@RequestMapping(value="/addUser")
public String addUser(@ModelAttribute User user) {}
6.通过HttpServletRequest接收
@RequestMapping("/addUser")
public String addUser(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
}
网友评论