乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      聊聊@RequestMapping和@GetMapping @PostMapping的區(qū)別

       昵稱10087950 2022-06-16 發(fā)布于江蘇

      最近學(xué)習(xí)看一些代碼,發(fā)現(xiàn)對于發(fā)送請求這件事,有的地方用@RequestMapping,有的地方用@PostMapping,為了搞清楚區(qū)別,特意查了下spring 源代碼,現(xiàn)在特此記錄下。

      • @GetMapping用于將HTTP get請求映射到特定處理程序的方法注解
      • 具體來說,@GetMapping是一個組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。
      • @PostMapping用于將HTTP post請求映射到特定處理程序的方法注解
      • 具體來說,@PostMapping是一個組合注解,是@RequestMapping(method = RequestMethod.POST)的縮寫。

      下面我們來看下@GetMapping的源碼

      可以對上面的兩句釋義給予充分的支撐。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      /**
       * Annotation for mapping HTTP {@code GET} requests onto specific handler
       * methods.
       *
       * <p>Specifically, {@code @GetMapping} is a <em>composed annotation</em> that
       * acts as a shortcut for {@code @RequestMapping(method = RequestMethod.GET)}.
       *
       *
       * @author Sam Brannen
       * @since 4.3
       * @see PostMapping
       * @see PutMapping
       * @see DeleteMapping
       * @see PatchMapping
       * @see RequestMapping
       */
      @Target(ElementType.METHOD)
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @RequestMapping(method = RequestMethod.GET)
      public @interface GetMapping {
        
       /**
        * Alias for {@link RequestMapping#name}.
        */
       @AliasFor(annotation = RequestMapping.class)
       String name() default "";
          ...
      }

      上面代碼中,最關(guān)鍵的是

      1
      @RequestMapping(method = RequestMethod.GET)

      這行代碼即說明@GetMapping就是@RequestMapping附加了請求方法。

      同時,可以看到@GetMapping這個注解 是spring4.3版本引入,同時引入的還有@PostMapping、@PutMapping、@DeleteMapping和@PatchMapping,一共5個注解。

      所以,一般情況下用

      1
      @RequestMapping(method = RequestMethod. XXXX)

      即可。

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

      介紹幾種如何處理url中的參數(shù)的注解@PathVaribale/@RequestParam/@GetMapping。

      其中,各注解的作用為:

      @PathVaribale 獲取url中的數(shù)據(jù)

      @RequestParam 獲取請求參數(shù)的值

      @GetMapping 組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫

      看一個例子,如果我們需要獲取Url=localhost:80/consumer/get/{id}中的返回的dept值,實現(xiàn)代碼如下:

      以上,通過@PathVariable注解來獲取URL中的時參數(shù)的前提條件是我們知道url的格式時怎么樣的。

      只有知道url的格式,我們才能在指定的方法上通過相同的格式獲取相應(yīng)位置的參數(shù)值。

      一般情況下,url的格式為:localhost:80/consumer/get/{id},這種情況下該如何來獲取其中的返回的dept值呢,

      關(guān)于@RequestParam來完成獲取返回值代碼如下

      當(dāng)輸入:http://localhost/consumer/dept/get/1?id=1

      看到返回了dept的結(jié)果:

      但是當(dāng)輸入:http://localhost/consumer/dept/get/1 (即不輸入id參數(shù)和參數(shù)值)

      但是當(dāng)輸入:http://localhost/consumer/dept/get/1?id (不輸入id參數(shù)值)

      會報如下錯誤:

      @RequestParam注解給我們提供了這種解決方案,即允許用戶不輸入id時,使用默認值,具體代碼如下:

      此時輸入:http://localhost/consumer/dept/get/1?id 就不在報錯(使用了默認值)

      輸入:http://localhost/consumer/dept/get/1

      @GetMapping 組合注解

      @GetMapping是一個組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。

      即可以使用@GetMapping(value = “/dept/get/{id}”)來代替

      1
      @RequestMapping(value=”/dept/get/{id}”,method= RequestMethod.GET)

      即可以讓我們精簡代碼。

      輸入:http://localhost/consumer/dept/get/1?id

      輸入:http://localhost/consumer/dept/get/1

      小結(jié)

      本篇文章介紹了幾種常用獲取url中的參數(shù)哈,比較簡單。以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多