欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法

 更新時(shí)間:2024年04月28日 11:07:27   作者:daydreamed  
這篇文章主要介紹了Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友參考下吧

@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)文章

最新評(píng)論