spring boot http 參數

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
    package com.example.yzm.demo2;
    
    
    import org.springframework.web.bind.annotation.*;
    import javax.servlet.http.HttpServletResponse;
    
    @RestController
    public class MyController {
        @RequestMapping("/hello")
        public String sayHello(@RequestParam(value = "name") String name) {
            return "Hello " + name + "!";
        }
    
        @RequestMapping(value = "/hello/yzm", method = RequestMethod.GET)
        public String sayHello2(HttpServletResponse rsp, @RequestParam("id") Integer id, @RequestParam("name") String name) {
            return "id:" + id + " name:" + name;
        }
        
        // 修改默認集
        @RequestMapping(value = {"/hello/test"}, method = {RequestMethod.POST}, produces = "application/json;charset=gb2312")
        @ResponseBody
        public String cancelCall(HttpServletResponse rsp) {
            rsp.addHeader("Access-Control-Allow-Origin", "*");
            return "{\"url\":\"http://192.168.1.1/notify\"}";
        }
    
    }
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章