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

@PathVaribale  获取url中的数据

@RequestParam  获取请求参数的值

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

(1)PathVaribale 获取url中的数据

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

 @RestController
public 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;
}
}

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

id:81

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

 @RestController
public 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;
}
}

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

id:100 name:helloworld

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

  

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

  例如:

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

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

id:1000

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

id:null

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

 whitelable Error Page错误 

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

 @RestController
public 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;
}
}

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

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

在浏览器中的测试结果如下: 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) 。即可以让我们精简代码。

 @RestController
public 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;
}
}

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. Idea Cannot import to svn: Cannot run program "svn"

    svn 出此问题:意味着不可检出代码. 按此修改,重启IDEA即可检出svn代码.

  2. 工作总结 string类型保存 "" 这种类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 36氪Plus消息:贷款组合推荐工具“钱小二”已获数百万天使轮融资

    根据"钱小二"在创业者社区36氪plus上的更新信息,该团队已获得数百万天使轮融资,投资方及投资时间不明. "钱小二"是一个贷款搜索 + 贷款组合推荐平台,目标 ...

  4. Predix中模型设计

    GE的Predix使用了图形数据库作为Asset存储,用以解决传统RDBMS系统中扩展性差,不支持行的动态定义问题. 对于实体,或者Asset来说,由如下几个方面表述: ID: Ties everyt ...

  5. VS2015 解决方案 或者项目 卡 正在加载 的解决办法

    导致项目无法打开以及VS无法关闭. 解决方法: 1.关闭VS: 2.去C:\Users\<your users name>\AppData\Local\Microsoft\VisualSt ...

  6. First MFC

    // stdafx.h : include file for standard system include files, // or project specific include files t ...

  7. Python内置函数之ascii()

    ascii()返回一个字符串对象. ascii()的参数只能有一个. 如果参数中有非ascii字符,会用 \u,\U,\x 来替代. ascii()和Python2中repr()等效 下面看看例子: ...

  8. Java并发编程(十三)在现有的线程安全类中添加功能

    重用现有的类而不是创建新的类,可以降低工作量,开发风险以及维护成本. 有时候线程安全类可以支持我们所有的操作,但更多时候,现有的了类只能支持大部分的操作,此时就需要在不破坏线程安全性的情况下添加一个新 ...

  9. Java并发编程(六)发布与逸出

    "发布(Publish)"一个对象的意思指,使对象能够在作用域之外的代码中使用. 例如: 将一个指向该对象的引用保存到其他代码可以访问的地方 在一个非私有的方法中返回该引用 将引用 ...

  10. ansible学习之--安装Svn

    1.安装svn 机器 Ubuntu SMP Thu Jan 15 20:21:55 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 使用 sudo apt-get in ...