@RequestParam
GET和POST请求传递参数会自动转换赋值到@RequestParam 所注解的变量上
- @RequestParam(org.springframework.web.bind.annotation.RequestParam)用于将指定的请求参数赋值给方法中的形参。
get请求:
url:http://localhost:8080/urlTest?tagId=1&pageIndex=3
RequestMapping(value="/urlTest", method = RequestMethod.GET)
public String urlTest(@RequestParam(value="tagId") String tagId, @RequestParam(value="pageIndex") String pageIndex){
System.out.println("urlTest");
System.out.println("tagId: " + tagId);
System.out.println("pageIndex: " + pageIndex);
return "hello";
}
上面请求是两个参数都得传递,因为@RequestParam注解有个required属性,它默认为true,标记参数必须传递。
如果两个参数是可传可不传那需要对之前方法改进
get请求:
url:http://localhost:8080/urlTest?tagId=1 传递第一个参数
url:http://localhost:8080/urlTest?pageIndex=3 传递第二个参数
url:http://localhost:8080/urlTest?tagId=1&pageIndex=3 两个参数都传递
RequestMapping(value="/urlTest", method = RequestMethod.GET)
public String urlTest(@RequestParam(value="tagId",required = false, defaultValue = "") String tagId, @RequestParam(value="pageIndex",required = false, defaultValue = "") String pageIndex){
System.out.println("urlTest");
System.out.println("tagId: " + tagId);
System.out.println("pageIndex: " + pageIndex);
return "hello";
}
- value
必须和URL传递参数名一样
- required
标识参数是否必须传递
- defaultValue
设置参数默认值
接收请求参数的方式:
一:@RequestParam(value=“username”) String userName, @RequestParam(value=“usernick”) String userNick //value中的参数名称要跟name中参数名称一致
二:String username, String usernick// 此时要参数名称一致
三:HttpServletRequest request //request.getParameter(“usernick”)
post请求:
跟get请求格式一样,只是把方法中的method = RequestMethod.GET换成method = RequestMethod.POST
@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。提交方式为get或post。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
RequestParam实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。
get方式中query String的值,和post方式中body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到。
@RequestBody
@RequestBody注解可以接收json格式的数据,并将其转换成对应的数据类型。
- @RequestBody接收一个对象
url请求:http://localhost:8080/query
@RequestMapping(value="/query", method = RequestMethod.POST)
public Entity query(@RequestBody Entity entity){
System.out.println("entity: " + entity.toString());
System.out.println("entity name: " + entity.getTitle());
return entity;
}
@RequestBody也可以接收不同的字符串
$.ajax({
type: "post",
url: "http://localhost:8080/query",
datatype: {
id:this.id, //绑定ID的值
name:this.name //绑定name的值
},
success: function (data) {
alert('成功');
}, error: function () {
alert('失败');
}
@RequestMapping(value="/query", method = RequestMethod.POST)
public Map query(@RequestBody Map<String,String> model){
System.out.println("entity Id:" + model.get("id"));
System.out.println("entity name: " + model.get("name"));
return entity;
}
GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。
@RequestBody用于post请求,不能用于get请求
创建一个新的entity,将两个entity都进去。这是最简单的
用Map<String, Object>接受request body,自己反序列化到各个entity中
@ModelAttribute
@ModelAttribute注解类型将参数绑定到Model对象
当你用GET请求传递实体时@RequestBody是接收不到的,这时需要ModelAttribute接收,实际代码参考@RequestBody写法。只需要替换为@ModelAttribute即可。
@PathVariable
支持get和post两种请求,大多数都会使用这种方法
url请求:http://localhost:8080/query/1/2/3
@GetMapping(value = "query/{id}/{ids}/{idno}")
public void queryJudgeQues(@PathVariable String id,@PathVariable String ids,@PathVariable String idno){
具体实现代码
}