@RequestParam comment
As the name implies: acquisition parameters, i.e., is transmitted from the acquisition parameters; e.g. the acquiring the link id
parameter value:
//链接(注意链接格式区别)
http://localhost:8090/hello?id=2
//使用@RequestParam注解获取id
public String Demo1(@RequestParam String id){ System.out.println("链接中请求参数的id:"+id); return null; }
At this time, @RequestParam
the role can obtain id and down as the argument to the method body shaped inside id
@PathVariable comment
As the name suggests: path variable, that is, the link acquisition variable path, such as access to the following links id
:
//链接(注意比较上面一条链接)
http://localhost:8090/hello/2
//使用@PathVariable注解获取id
@RequestMapping(value = "/getBook/{id}", method = RequestMethod.GET) public String getBook(@PathVariable Integer id) { try { system.out.println("路径上的id:"+id); } catch (ParseException e) { e.printStackTrace(); } return null; }
At this @PathVariable
action is to obtain the path id passed to the method to come inside the body parameter id, but the variable name must be the same, such as where: value = "/getBook/{id}"
and @PathVariable Integer id;
two are to be id, if different error;
@RequestBody comment
First of all that, the @RequestBody
annotation process is generally mainly used content-type:"application/json charset=utf-8"
or content-type:"application/xml charset=utf-8"
two kinds of request data, is generally used in Comparative asynchronous request more, for example:
//异步请求部分代码
$.ajax({
url:"/hello",
type:"POST", data:'{"id":"123","name":"chenyc"}', content-type:"application/json charset=utf-8", success:function(data){ alert(data); } });
//@requestBody注解获取数据代码
@requestMapping("/hello")
public String hello(@requestBody Integer id,@requestBody String name){ System.out.println("id:"+id+";"+"name:"+name); }
At this @requestBody
annotation can be obtained various parameters in the request is then assigned to the corresponding process parameter, Further, when there is an entity class User
contains id
and name
elements, then, in which the method can be directly written @requestBody User user
good automatically package for our use of, do not trouble like this @requestBody Integer id,@requestBody String
name one by one package