1.介绍几种如何处理url中的参数的注解

@PathVaribale  获取url中的数据

@RequestParam  获取请求参数的值

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

(1)PathVaribale 获取url中的数据

  看一个例子,如果我们需要获取Url=localhost:8080/hello/id中的id值,实现代码如下:

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

在浏览器中 输入地址: localhost:8080/hello/100/helloworld 然后会在html页面上打印出:

id:81

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

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

在浏览器中输入地址: localhost:8080/hello/100/helloworld 然后会在html页面上打印出:

id:100 name:helloworld

  以上,通过 @PathVariable 注解来获取URL中的参数时的前提条件是我们知道url的格式时怎么样的。
  只有知道url的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。
  一般情况下,url的格式为: localhost:8080/hello?id=98 ,这种情况下该如何来获取其id值呢,这就需要借助于 @RequestParam 来完成了

  

2.@RequestParam 获取请求参数的值

  例如:

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

在浏览器中输入地址: localhost:8080/hello?id=1000 ,可以看到如下的结果:

id:1000

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

id:null

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

 whitelable Error Page错误 

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

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

如果在url中有多个参数,即类似于 localhost:8080/hello?id=98&&name=helloworld 这样的url,同样可以这样来处理。具体代码如下:

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

在浏览器中的测试结果如下: localhost:8080/hello?id=1000&name=helloworld 地址,就会显示下面的内容:
 id:1000 name:helloworld

3.@GetMapping 组合注解

@GetMapping 是一个组合注解,是 @RequestMapping(method = RequestMethod.GET) 的缩写。该注解将HTTP Get 映射到 特定的处理方法上。
即可以使用 @GetMapping(value = “/hello”) 来代替 @RequestMapping(value=”/hello”,method= RequestMethod.GET) 。即可以让我们精简代码。

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

4.PostMapping组合注解:

  方法同GetMapping

参考:http://blog.csdn.net/u010412719/article/details/69788227

SpringBoot处理url中的参数的注解的更多相关文章

  1. 【转】SpringBoot处理url中的参数的注解

    1.介绍几种如何处理url中的参数的注解 @PathVaribale  获取url中的数据 @RequestParam  获取请求参数的值 @GetMapping  组合注解,是 @RequestMa ...

  2. 如何获取url中的参数并传递给iframe中的报表

    在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...

  3. JavaScript如何获取网页url中的参数

    我们可以自定义一个公共函数来实现网页url中的参数获取,返回的是一个数组 GetUrlRequest: function () { var url = decodeURI(location.searc ...

  4. 使用JS,获取URL中指定参数的值

    /** * 获取URL中指定参数的值 * * @param name 参数名称 * @returns */ function getQueryString(name) { var reg = new ...

  5. APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法

    用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...

  6. 通过Javascript得到URL中的参数(query string)

    我们知道,"GET"请求中,通常把参数放在URL后面,比如这样http://www.cnblogs.com/season-huang/index?param=yes&art ...

  7. javaScript获取url中的参数

    var urlTools = { //获取RUL参数值 getUrlParam: function(name) { /*?videoId=identification */ var params = ...

  8. ${param.xxx}获取url中的参数

    在项目中看到了一个很奇怪的EL表达式...${param.catid}...一直找不到param在哪里定义的...(主要是水平太屎...) 然后从网上查了一下才知道是啥... EL表达式${param ...

  9. vue中如何不通过路由直接获取url中的参数

    前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...

随机推荐

  1. 转FTP协议详解

    转自:http://www.cnblogs.com/li0803/archive/2010/11/16/1878833.html FTP 是File Transfer Protocol(文件传输协议) ...

  2. 字符串算法之 AC自己主动机

    近期一直在学习字符串之类的算法,感觉BF算法,尽管非常easy理解,可是easy超时,全部就想学习其它的一些字符串算法来提高一下,近期学习了一下AC自己主动机.尽管感觉有所收获,可是还是有些朦胧的感觉 ...

  3. tony_CENTOS启动方式设置

    方法: 在etc文件夹下面有个初始加载文件是用来启动系统的,系统在启动的时候先去env中找到shell的必要配置,然后把shell启动起来,那么再然后就要启动整个系统了,到底是启动图形界面呢,还是字符 ...

  4. Linux在本地使用yum安装软件

    经常遇到有的linux服务器由于特殊原因,不能连接外网,但是经常需要安装一些软件,尤其是在编译一些包的时候经常由于没有安装一些依存包而报的各种各样的错误,当你找到依存的rpm包去安装的时候,又提示你有 ...

  5. VS代码注释插件GhostDoc

    http://community.submain.com/blogs/tutorials/archive/2013/03/28/how-to-access-ghostdoc-pro-features. ...

  6. CentOS7中关闭selinux

    在安装Cobbler和Puppet时需要关闭selinux,但是通常情况下载安装完CentOS7后,默认情况下SElinux是启用状态, 如下所示: [csharp] view plaincopy   ...

  7. DSP6455的cmd文件

    DSP6455的cmd文件 CMD 的专业名称叫链接器配置文件,存放链接器的配置信息,DSP编译器的编译结果是未定位的,DSP也没有操作系统来定位执行代码,DSP系统的配置需求也不尽相同,因此需要定义 ...

  8. Avira Free Antivirus 小红伞免费杀毒软件广告去除工具

    Avira Free Antivirus 小红伞免费杀毒软件经常跳出广告, 用起来比较烦, 这里提供一个广告去除的免费小工具. 原理就是用组策略来阻止广告的跳出, 网上到处都是. 一键傻瓜式去除, 也 ...

  9. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  10. C++中两个类相互include的问题

    在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 例如: Cup.h #ifndef CUP_H #define CUP_ ...