Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法
@RequestParam、@RequestBody、@RequestPart 對(duì)比
請(qǐng)求體格式:
- @RequestParam:application/x-www-form-urlencoded
- @RequestBody:application/json、application/xml
- @RequestPart:multipart/form-data
入?yún)㈩愋停?/p>
- @RequestParam:基本類型、String、MultipartFile
- @RequestBody:k-v
- @RequestPart:k-v,MultipartFile
注解屬性
- @RequestParam:value/name、required、defaultValue
- @RequestBody:required
- @RequestPart:value/name、required
匹配解析器:
/** * Find a registered {@link HandlerMethodArgumentResolver} that supports * the given method parameter. */ @Nullable private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) { HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter); if (result == null) { for (HandlerMethodArgumentResolver resolver : this.argumentResolvers) { if (resolver.supportsParameter(parameter)) { result = resolver; this.argumentResolverCache.put(parameter, result); break; } } } return result; }
@RequestHeader、@RequestAttribute、@PathVariable、@CookieValue、@SessionAttribute 對(duì)比
作用:
@RequestHeader:用于接收請(qǐng)求頭中的參數(shù)
@RequestAttribute:用于接收上一個(gè)請(qǐng)求中設(shè)置的參數(shù)
@PathVariable:用于接收路徑中的參數(shù)
@CookieValue:用于接收 Cookie 中的參數(shù)
@SessionAttribute:用于接收 Session 中的參數(shù)
用例:
// 1 @RequestParam // 1.1 指定屬性名 @GetMapping("/test") public Result test(@RequestParam("param") String param); // 1.2 指定非必傳,默認(rèn)是必傳 @GetMapping("/test") public Result test(@RequestParam(value = "param", required = false) String param); // 2 @RequestBody @PostMapping("/test") public Result test(@RequestBody Map<String, Object> params); // 3 @RequestPart @PostMapping("/test") public Result test(@RequestParam("file") MultipartFile file, @RequestPart("params") Map<String, Object> params); // 4 @RequestHeader @GetMapping("/test") public Result test(@RequestHeader("param") String param); // 5 @RequestAttribute @GetMapping("/test") public Result test(@RequestAttribute("param") String param); // 6 @PathVariable @GetMapping("/test/{param}") public Result test(@PathVariable("param") String param); // 7 @CookieValue @GetMapping("/test") public Result test(@CookieValue("param") String param); // 8 @SessionAttribute @GetMapping("/test") public Result test(@SessionAttribute("param") String param);
到此這篇關(guān)于Spring 處理 HTTP 請(qǐng)求參數(shù)注解的文章就介紹到這了,更多相關(guān)Spring HTTP 請(qǐng)求注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程(單例模式,堵塞隊(duì)列,定時(shí)器)詳解
這篇文章主要介紹了java多線程的(單例模式,堵塞隊(duì)列,定時(shí)器),具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下2021-08-08詳解Java線程池如何統(tǒng)計(jì)線程空閑時(shí)間
這篇文章主要和大家分享一個(gè)面試題:Java線程池是怎么統(tǒng)計(jì)線程空閑時(shí)間?文中的示例代碼講解詳細(xì),對(duì)我們掌握J(rèn)ava有一定幫助,需要的可以參考一下2022-11-11spring-cloud入門之spring-cloud-config(配置中心)
這篇文章主要介紹了spring-cloud入門之spring-cloud-config(配置中心),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01生成PDF全攻略之在已有PDF上添加內(nèi)容的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇生成PDF全攻略之在已有PDF上添加內(nèi)容的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06JavaGUI實(shí)現(xiàn)隨機(jī)單詞答題游戲
這篇文章主要為大家詳細(xì)介紹了JavaGUI實(shí)現(xiàn)隨機(jī)單詞答題游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12Java微信二次開發(fā)(二) Java微信文本消息接口請(qǐng)求與發(fā)送
這篇文章主要為大家詳細(xì)介紹了Java微信二次開發(fā)第二篇,Java微信文本消息接口請(qǐng)求與發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Java中Hashtable類與HashMap類的區(qū)別詳解
Hashtable的應(yīng)用非常廣泛,HashMap是新框架中用來(lái)代替Hashtable的類,也就是說(shuō)建議使用HashMap,不要使用Hashtable??赡苣阌X(jué)得Hashtable很好用,為什么不用呢?這里簡(jiǎn)單分析他們的區(qū)別。2016-01-01springboot如何設(shè)置請(qǐng)求參數(shù)長(zhǎng)度和文件大小限制
這篇文章主要介紹了springboot如何設(shè)置請(qǐng)求參數(shù)長(zhǎng)度和文件大小限制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09