SpringBoot处理url中的参数的注解
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中的参数的注解的更多相关文章
- 【转】SpringBoot处理url中的参数的注解
1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMa ...
- 如何获取url中的参数并传递给iframe中的报表
在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...
- JavaScript如何获取网页url中的参数
我们可以自定义一个公共函数来实现网页url中的参数获取,返回的是一个数组 GetUrlRequest: function () { var url = decodeURI(location.searc ...
- 使用JS,获取URL中指定参数的值
/** * 获取URL中指定参数的值 * * @param name 参数名称 * @returns */ function getQueryString(name) { var reg = new ...
- APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法
用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...
- 通过Javascript得到URL中的参数(query string)
我们知道,"GET"请求中,通常把参数放在URL后面,比如这样http://www.cnblogs.com/season-huang/index?param=yes&art ...
- javaScript获取url中的参数
var urlTools = { //获取RUL参数值 getUrlParam: function(name) { /*?videoId=identification */ var params = ...
- ${param.xxx}获取url中的参数
在项目中看到了一个很奇怪的EL表达式...${param.catid}...一直找不到param在哪里定义的...(主要是水平太屎...) 然后从网上查了一下才知道是啥... EL表达式${param ...
- vue中如何不通过路由直接获取url中的参数
前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...
随机推荐
- jQuery OCUpload ——> 一键上传插件
OCUpload为jQuery的插件(One Click Upload),意思为一键上传,封装了对于文件上传的一些方法,只需几行代码,文件上传优雅而简洁. 对于传统的文件上传,只是通过inp ...
- iOS开发-XCode常用快捷键整理
前言:如果我们能够掌握并巧妙地使用快捷键,可以大大加快我们的工作效率,这个对经常使用快捷键的人们来说,应该很容易理解.因此我们需要做的是,针对于自己经常使用的快捷键去进行记忆.我不会推荐你们去把所有的 ...
- html标签说明
dictype 不区分大小写 HTML 4.01 与 HTML5 之间的差异 在 HTML 4.01 中有三种 <!DOCTYPE> 声明.在 HTML5 中只有一种: <!DOCT ...
- linux学习一个服务(未完)
学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)
- SpringBoot整合Quartz
1.引入quzrtz <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId> ...
- hive中的join
建表 : jdbc:hive2://localhost:10000> create database myjoin; No rows affected (3.78 seconds) : jdbc ...
- FreeBSD编译安装emacs,不要用ports
1. 解压emacs 2. 进入解压之后的目录,执行configure命令,大体配置如下: ./configure --with-x-toolkit=no --with-xpm=no --with-j ...
- Python 安装 MaxMind GeoLite City
1.先安装 geoip c library geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed ...
- Linux 进程创建二(execve和wait)
三:execve系统调用 int execve(const char *filename, char *const argv[],char *const envp[]); fork创建了一个新的进程, ...
- php5.4n 长链接 bug
运行5.2的部分程序会有中文无法显示的问题 恢复到5.2后中文显示正常