zoukankan      html  css  js  c++  java
  • SpringBoot处理url中的参数的注解

    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 获取请求参数的值

      例如:

    @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时,使用默认值,具体代码如下:

    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

  • 相关阅读:
    Spring-四种常用注解
    Spring-IOC
    Spring-bean 的管理细节
    Android R.java文件
    MySQL | windows10 安装MySQL : 无法将“mysql”项识别为 cmdlet、函数、脚本....
    统计知识 | 决定系数 R方、调整后的R方、F值
    R | R语言表达式中常用的符号
    回归分析 | R语言回归算法、模型诊断
    R | 探索性数据分析 EDA
    R语言基础 | 概率分布的表示方法
  • 原文地址:https://www.cnblogs.com/jin-zhe/p/8204000.html
Copyright © 2011-2022 走看看