SpringBoot中常用注解@ PathVaribale / @ RequestParam / @ GetMapping介绍 本篇博文将介绍几种如何处理URL中的参数的注解@ PathVariba

来源:互联网 发布:斑马标签机打印软件 编辑:程序博客网 时间:2024/05/21 13:57


文章来自 http://blog.csdn.net/u010412719/article/details/69788227

SpringBoot中常用注解@ PathVaribale / @ RequestParam / @ GetMapping介绍

本篇博文将介绍几种如何处理URL中的参数的注解@ PathVaribale / @ RequestParam / @ GetMapping。

其中,各注解的作用为:

@PathVaribale获取url中的数据

@RequestParam获取请求参数的值

@GetMapping组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写

@PathVaribale获取url中的数据

看一个例子,如果我们需要获取URL =本地主机:8080 /你好/ ID中的ID值,实现代码如下:

@RestControllerpublic class HelloController {    @RequestMapping(value="/hello/{id}",method= RequestMethod.GET)    public String sayHello(@PathVariable("id") Integer id){        return "id:"+id;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8

同样,如果我们需要在URL有多个参数需要获取,则如下代码所示来做就可以了。

@RestControllerpublic class HelloController {    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){        return "id:"+id+" name:"+name;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9

以上,通过@PathVariable注解来获取URL中的参数时的前提条件是我们知道网址的格式时怎么样的。

只有知道网址的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。

一般情况下,URL的格式为:本地主机:8080 /你好ID = 98,这种情况下该如何来获取其ID值呢,这就需要借助于@RequestParam来完成了

@RequestParam获取请求参数的值

直接看一个例子,如下

@RestControllerpublic class HelloController {    @RequestMapping(value="/hello",method= RequestMethod.GET)    public String sayHello(@RequestParam("id") Integer id){        return "id:"+id;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8

在浏览器中输入地址:本地主机:8080 /你好ID = 1000,可以看到如下的结果: 

当我们在浏览器中输入地址:localhost:8080 / hello?id,即不输入id的具体值,此时返回的结果为null。具体测试结果如下:

但是,当我们在浏览器中输入地址:localhost:8080 / hello,即不输入id参数,则会报报如下错误:

@RequestParam注解给我们提供了这种解决方案,即允许用户不输入ID时,使用默认值,具体代码如下:

@RestControllerpublic class HelloController {    @RequestMapping(value="/hello",method= RequestMethod.GET)    //required=false 表示url中可以不穿入id参数,此时就使用默认参数    public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){        return "id:"+id;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8

测试结果如下; 

如果在URL中有多个参数,即类似于本地主机:8080 /你好ID = 98 &&名= wojiushimogui这样的链接,同样可以这样来处理具体代码如下:

/** * Created by wuranghao on 2017/4/7. */@RestControllerpublic class HelloController {    @RequestMapping(value="/hello",method= RequestMethod.GET)    public String sayHello(@RequestParam("id") Integer id,@RequestParam("name") String name){        return "id:"+id+ " name:"+name;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在浏览器中的测试结果如下:

@GetMapping组合注解

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get映射到特定的处理方法上。

即可以使用@GetMapping(value =“/ hello”)来代替@RequestMapping(value =“/ hello”,method = RequestMethod.GET),即可以让我们精简代码。

例子

@RestControllerpublic class HelloController {    //@RequestMapping(value="/hello",method= RequestMethod.GET)    @GetMapping(value = "/hello")    //required=false 表示url中可以不穿入id参数,此时就使用默认参数    public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){        return "id:"+id;    }}
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9

小结

本篇博文介绍了几种常用获取URL中的参数哈,比较简单。

阅读全文
0 0
原创粉丝点击