@RequestParam、@RequestBody和@ModelAttribute区别

@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格式的数据,并将其转换成对应的数据类型。

  1. @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){
    		具体实现代码
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值