springboot從零開始: @RequestMapping與@GetMapping區別

前言:

本系列博客記錄 springboot 求學之路:
springboot 有很多請求方式,這裏記錄一下如題的一個疑問,

1.概說
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

從名字不難看出這些是定義路由的訪問方式,get、post等,點進去查看他們的上層代碼,例如 @GetMapping,在最上面有幾行註釋,如下

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
    method = {RequestMethod.GET}
)
public @interface GetMapping {
    @AliasFor(
        annotation = RequestMapping.class
    )
    .......

這裏面有聲明 ,很明顯就是@RequestMapping的註解,並使用 get方法

@RequestMapping(
    method = {RequestMethod.GET}
)

到這裏幾乎就很明顯了,上面的幾個註解都是包含@RequestMapping的組合註解,簡化了原來的使用方法。
下面這兩個是等價的:

   @GetMapping(path = "/get")
   public String getName() {
       return "";
   }

   @RequestMapping(path = "/get", method = RequestMethod.GET)
   public String get() {
       return "";
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章