聊聊@RequestMapping和@GetMapping @PostMapping的區(qū)別
@RequestMapping和@GetMapping @PostMapping的區(qū)別
最近學(xué)習(xí)看一些代碼,發(fā)現(xiàn)對(duì)于發(fā)送請(qǐng)求這件事,有的地方用@RequestMapping,有的地方用@PostMapping,為了搞清楚區(qū)別,特意查了下spring 源代碼,現(xiàn)在特此記錄下。
@GetMapping
用于將HTTP get請(qǐng)求映射到特定處理程序的方法注解- 具體來(lái)說(shuō),@GetMapping是一個(gè)組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。
@PostMapping
用于將HTTP post請(qǐng)求映射到特定處理程序的方法注解- 具體來(lái)說(shuō),@PostMapping是一個(gè)組合注解,是@RequestMapping(method = RequestMethod.POST)的縮寫。
下面我們來(lái)看下@GetMapping的源碼
可以對(duì)上面的兩句釋義給予充分的支撐。
/** * 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)鍵的是
@RequestMapping(method = RequestMethod.GET)
這行代碼即說(shuō)明@GetMapping就是@RequestMapping附加了請(qǐng)求方法。
同時(shí),可以看到@GetMapping這個(gè)注解 是spring4.3版本引入,同時(shí)引入的還有@PostMapping、@PutMapping、@DeleteMapping和@PatchMapping,一共5個(gè)注解。
所以,一般情況下用
@RequestMapping(method = RequestMethod. XXXX)
即可。
SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介紹
介紹幾種如何處理url中的參數(shù)的注解@PathVaribale/@RequestParam/@GetMapping。
其中,各注解的作用為:
@PathVaribale 獲取url中的數(shù)據(jù)
@RequestParam 獲取請(qǐng)求參數(shù)的值
@GetMapping 組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫
看一個(gè)例子,如果我們需要獲取Url=localhost:80/consumer/get/{id}中的返回的dept值,實(shí)現(xiàn)代碼如下:
以上,通過(guò)@PathVariable注解來(lái)獲取URL中的時(shí)參數(shù)的前提條件是我們知道url的格式時(shí)怎么樣的。
只有知道url的格式,我們才能在指定的方法上通過(guò)相同的格式獲取相應(yīng)位置的參數(shù)值。
一般情況下,url的格式為:localhost:80/consumer/get/{id},這種情況下該如何來(lái)獲取其中的返回的dept值呢,
關(guān)于@RequestParam來(lái)完成獲取返回值代碼如下
當(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ù)值)
會(huì)報(bào)如下錯(cuò)誤:
@RequestParam注解給我們提供了這種解決方案,即允許用戶不輸入id時(shí),使用默認(rèn)值,具體代碼如下:
此時(shí)輸入:http://localhost/consumer/dept/get/1?id 就不在報(bào)錯(cuò)(使用了默認(rèn)值)
輸入:http://localhost/consumer/dept/get/1
@GetMapping 組合注解
@GetMapping是一個(gè)組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。
即可以使用@GetMapping(value = “/dept/get/{id}”)來(lái)代替
@RequestMapping(value=”/dept/get/{id}”,method= RequestMethod.GET)
即可以讓我們精簡(jiǎn)代碼。
輸入:http://localhost/consumer/dept/get/1?id
輸入:http://localhost/consumer/dept/get/1
小結(jié)
本篇文章介紹了幾種常用獲取url中的參數(shù)哈,比較簡(jiǎn)單。以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Spring Boot 中的 @PutMapping 注解原理及使用小結(jié)
- Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)
- Spring MVC @GetMapping和@PostMapping注解的使用方式
- 詳解SpringBoot中@PostMapping注解的用法
- Java @PostMapping和@GetMapping方法使用詳解
- SpringBoot @PostMapping接收HTTP請(qǐng)求的流數(shù)據(jù)問(wèn)題
- 如何解決@PutMapping或@PostMapping接收String類型參數(shù)多兩個(gè)“引號(hào)問(wèn)題
相關(guān)文章
Java MongoDB實(shí)現(xiàn)REST過(guò)程解析
這篇文章主要介紹了Java MongoDB實(shí)現(xiàn)REST過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot?整合?Quartz?定時(shí)任務(wù)框架詳解
這篇文章主要介紹了SpringBoot整合Quartz定時(shí)任務(wù)框架詳解,Quartz是一個(gè)完全由Java編寫的開源作業(yè)調(diào)度框架,為在Java應(yīng)用程序中進(jìn)行作業(yè)調(diào)度提供了簡(jiǎn)單卻強(qiáng)大的機(jī)制2022-08-08java以json格式向后臺(tái)服務(wù)器接口發(fā)送請(qǐng)求的實(shí)例
下面小編就為大家分享一篇java以json格式向后臺(tái)服務(wù)器接口發(fā)送請(qǐng)求的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01java.lang.FileNotFoundException 異常的正確解決方法(親測(cè)有效)
java.io.FileNotFoundException是一個(gè)在文件操作過(guò)程中常見的異常,它屬于IOException的一個(gè)子類,這篇文章主要介紹了java.lang.FileNotFoundException 異常的正確解決方法(親測(cè)有效),需要的朋友可以參考下2024-01-01