几个输入参数@ApiParam @PathVariable @RequestParam三者区别



1@ApiParam

是注解APi的参数,也就是用于swagger提供开发者文档,文档生成的注释内容

2.@RequestParam,是获取前端传递给后端的参数,可以是get方式,也可以是post方式。其中如果前端传递的参数和后端你接受的参数起的名字字段是一致的可以省略不写,也可以直接写@RequestParam String name,如果不一致一定要完整写,不然获取不到.

@GetMapping(value = "/getname")
public String getname(@RequestParam("name") String name) {//这个适用于发送请求的时候 http://localhost:8086/admin/springboot-demo- lpp1/getname/?lpp
	return "hello !!"+name;	
}

这个适用于发送请求的时候地址栏http://localhost:8086/admin/springboot-demo-lpp1/getname/?name=lpp通过问号对变量进行赋值

ajax请求代码

:

$('#buttonlpp').unbind().on('click', function () {
 $.ajax({
 url: 'http://localhost:8086/admin/springboot-demo-lpp1/getname',
 type: 'GET',
 data: {name: $("#name").val()}, //最后地址出现的:?name=传过来的值
      success:function(data){ 
 alert(data);
 }
 })
 });
}}

在处理方法入参处使用@RequestParam 可以把请求参数传递给请求方法


– value:参数名


– required:是否必须。默认为 true,表示请求参数中必须包含对应的参数,若不存在,将抛出异常

/** 
    * 如果 required = true 则表示请求参数对应的 字段 必须存在.如果不存在则会抛出异常<br/> 
    * @param firstName 可以为null 
    * @param lastName 不能为null .为null报异常 
     * @param age age字段表示如果没有 age 参数 则默认值为 0  
     * @return 
     */  
    @RequestMapping("/requestParam")  
    public String requestParam(@RequestParam(value="firstName",required=false)String firstName,  
            @RequestParam( value="lastName" ,required = true) String lastName,  
            @RequestParam(value="age",required = false ,defaultValue="0")int age) {  
       System.out.println("hello my name is " + (firstName == null ? "" : firstName)  
                                       + lastName + "," + age +" years old this year");  
       return "helloworld";  
    }  
@RequestParam @PathVariable 注解是用于从 request 中接收请求的,两个都可以接收参数,关键点不同的是 @RequestParam 是从 request 里面拿取值,而 @PathVariable 是从一个 URI 模板里面来填充

3.@PathVariable是获取get方式,url后面参数,进行参数绑定

@ApiOperation(value = "删除公告", notes = "删除公告", httpMethod = "POST")
    @RequestMapping(value = "/delete/{bisKey}", method = RequestMethod.POST)
    public RequestResult remove(@ApiParam(name = "bisKey", value = "需要删除的公告ids", required = true) @PathVariable String bisKey) {
地址栏 http://lcloud-api-dev.paic.com.cn/springboot-demo-lpp1/lpp/lpp   后面 lpp 是代表参数

java代码:

@RestController 
public class test {
	
	@GetMapping(value = "/lpp/{name}")  //这是适用于post地址栏填写参数
	public String name(@PathVariable("name") String name) {
	return "hello !!"+name;
	}
}

发送ajax的代码:

$('#buttonlpp').unbind().on('click', function () {
var name=$("#name").val();
alert("输入值是"+name);
$.ajax({
params:{name},
url: 'http://localhost:8086/admin/springboot-demo-lpp1/lpp/'+name,
type: 'GET',
     success:function(data){ 
alert(data);
}
})
});
}}

4@PathParam

这个注解是和springpathVariable是一样的,也是基于模板的,但是这个是jboss包下面的一个实现,上面的是spring的一个实现,都要导包

界面地址栏http://localhost:8086/admin/springboot-demo-lpp1/getname1/?name=00

5@ResponseBody

responseBody表示服务器返回的时候以一种什么样的方式进行返回, 将内容或对象作为 HTTP 响应正文返回,值有很多,一般设定为json

6@requestMapping

@requestMapping  可以定义在 类 和 方法 上

@RequestMapping限定请求方法、请求参数、请求头

/** 
    * 接收GET请求 
     * @return 
     */  
    @RequestMapping(value="/get",method = RequestMethod.GET)  
    public String get(){  
       System.out.println("get");  
        return "get";  
    }  
  
   /** 
    * 接收POST请求 
    * @return 
     */  
   @RequestMapping(value="/post",method = RequestMethod.POST)  
   public String post(){  
       System.out.println("post");  
       return "post";  
   }  
     
    /** 
     * 只接收 name 参数 
    * @return 
     */  
    @RequestMapping(value="/params",params="name")  
    public String params(String name){  
        System.out.println("hello "+name);  
        return "helloworld";  
   }  
     
    /** 
     * 只接收请求头中 Content-Type 为 text/html;charset=UTF-8的请求 
    * @return 
    */  
    @RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")  
    public String headers(){  
        System.out.println("headers");  
        return "helloworld";  
    }  

7@RequestHeader 获取请求头

 

请求头包含了若干个属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将求头中的属性值绑定到处理方法的入参中

/** 
 * 获取请求头中的信息 
 * @RequestHeader 也有 value ,required ,defaultValue 三个参数 
 * @param userAgent 
 * @param cookie 
 * @return 
 */  
@RequestMapping("/requestHeader")  
public String requestHeader(@RequestHeader("User-Agent")String userAgent,@RequestHeader("Cookie")String cookie){  
   System.out.println("userAgent:["+userAgent+"]");  
    System.out.println("cookie:["+cookie+"]");  
   return "helloworld";  
}  
打印的结果:
userAgent:[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36]
cookie:[Idea-52c263bd=e00710d5-ce1d-498e-b78c-9845020b5369; BIGipServerPOOL_PACLOUD_STGR2017092707057=3397911582.24448.0000; BIGipServerPOOL_PACLOUD_DEVR2017101207499=374675722.63878.0000]
界面地址: http://localhost:8086/admin/springboot-demo-lpp1/requestHeader








  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值