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

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

其中,各注解的作用为:

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值

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

@PathVaribale 获取url中的数据

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

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

同样,如果我们需要在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; } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

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

一般情况下,url的格式为:localhost:8080/hello?id=98,这种情况下该如何来获取其id值呢,这就需要借助于@RequestParam来完成了

@RequestParam 获取请求参数的值

直接看一个例子,如下

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

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

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

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

@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; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

测试结果如下; 

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

/*** Created by wuranghao on 2017/4/7.*/
@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; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

@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; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

小结

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

转载于:https://www.cnblogs.com/williamjie/p/9200643.html

SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍相关推荐

  1. java常用注解及功能_SpringBoot 中常用注解及各种注解作用

    本篇文章将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  2. java参数值注入_在springboot中使用注解将值注入参数的操作

    后端的许多管理系统需要登陆者的信息,如shiro登陆后,会将登陆者的信息存储在shiro的session,在使用时需要多行代码获取用户信息.可以把获取在shiro中的登陆者信息封装在一个类中,使用时获 ...

  3. 声明式事务、Spring 中常用注解、Ajax

    五. 声明式事务 编程式事务: 1.1 由程序员编程事务控制代码. 1.2 OpenSessionInView 编程式事务 声明式事务: 先引入依赖 <dependency><gro ...

  4. 注解以及Java中常用注解使用

    元注解有六个: @Target(表示该注解可以用于什么地方). @Retention(表示再什么级别保存该注解信息). @Documented(将此注解包含再javadoc中). @Inherited ...

  5. springboot中得注解_Spring以及SpringBoot中的常用的注解小结

    一.注解的基本概念 Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法.Annotion(注解)是一个接口,程序可以通过反射来获 ...

  6. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

  7. Spring-boot框架常用注解

    springboot的核心就是注解.springboot通过各种组合注解,极大地简化了spring项目的搭建和开发.采用纯java代码,不在需要配置繁杂的xml文件.类型安全对重构可以提供良好的支持. ...

  8. @Transactional +自定义注解不生效_SpringBoot之路(三)SpringDataJpa中常用注解的使用...

    1 @Query注解详解及其用法 说明:本文的写作构建在笔者的SpringBoot之路(二)使用用Spring-Data-JPA访问数据库进行基本的CRUD操作这篇文章的基础之上. @Query注解在 ...

  9. SpringBoot中condition注解的使用

    在项目中,有时会遇到我们的Configuration.Bean.Service等等的bean组件需要依条件按需加载的情况. springboot中提供了一系列@Condition* 注解来处理有条件注 ...

最新文章

  1. 空间直角坐标系与球面坐标互转
  2. 边缘数据中心维护的4个基本组件
  3. xcode symbol(s) not found for architecture i386错误解决方法
  4. Java提升篇:理解String 及 String.intern() 在实际中的应用
  5. 错误/异常:java.io.FileNotFoundException: .\src\db.properties (系统找不到指定的路径。);的解决方法...
  6. 两个同时comet matlab,Matlab讲义 - 图文
  7. ocLazyLoad angular 按需加载
  8. shiro认证授权过程
  9. JS里设控件不可用取值
  10. 仅109美元 搞一套Evive物联网开发工具包回家耍
  11. bzoj3714 [PA2014]Kuglarz
  12. Python访问MySQL数据库速度慢解决方法
  13. 用C语言做九九乘法表
  14. MySQL日志之Undo日志
  15. PbX QDSC稳定性增强的策略及其氧化和钝化对PbSe量子点的影响
  16. 电脑声卡或者耳机接口坏了怎么办
  17. 智能网联建设核心评价指标探讨
  18. 修改mac地址导致计算机无法上网,win7系统更换MAC地址解决无法连接网络问题的解决方法...
  19. 席绢言情系列书评总序
  20. 江苏省高校,中专校职称计算机信息技术应用能力考核,江苏省高校中专校专业技术人员职称信息技术应用能力考核资料.doc...

热门文章

  1. 51nod 1011 最大公约数GCD
  2. 设计中色彩的注意事项有哪些
  3. win7下nsis打包exe安装程序教程
  4. freemodbus线圈中的位操作
  5. Android 动态类加载实现免安装更新
  6. Python -- 使用模块中的函数
  7. Git show-branch显示提交信息
  8. module_init 详解
  9. 2013.10u-boot移植之增加nand保存环境变量
  10. qstring转qchar_Qt 对QString操作