亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧分享

 更新時間:2023年05月27日 11:42:12   作者:染〞夏  
最近一直在學(xué)習(xí)Spring Boot,今天將其中的從前臺過來的參數(shù)傳遞總結(jié)一下,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

一:四種傳參方式

SpringBoot 接收參數(shù)的常用方式主要有以下幾種:

1.1:在 URL 中傳遞參數(shù)

可以通過在 URL 中添加參數(shù)來傳遞數(shù)據(jù),例如:/user?id=123&name=Tom。在 SpringBoot 中,可以使用 @RequestParam 注解來獲取請求參數(shù)。

1.2:PathVariable 傳遞參數(shù)(Restful 風(fēng)格)

PathVariable 可以接受 URL 中的變量值,例如:/user/123,其中 123 就是一個變量。在 SpringBoot 中,可以使用 @PathVariable 注解來獲取 URL 中的變量值。

1.3:在請求體中傳遞參數(shù)

可以將參數(shù)放在請求體中傳遞,例如:POST 請求中的表單數(shù)據(jù)或 JSON 數(shù)據(jù)。在 SpringBoot 中,可以使用 @RequestBody 注解來獲取請求體中的參數(shù)。

1.4:在請求頭中傳遞參數(shù)

可以在請求頭中添加參數(shù),例如:JWT Token。在 SpringBoot 中,可以使用 @RequestHeader 注解來獲取請求頭中的參數(shù)。

二:文件上傳接口測試  

2.1 : test.java

 @PostMapping("/test10")
    public Result test10(@RequestParam("file") MultipartFile file) {
        return Result.ok(200);
    }

三、@RequestParam

3.1 多個參數(shù)

  @GetMapping("/test3")
    public Result test3(@RequestParam("id") Integer id ,
                        @RequestParam("name") String name){
        return Result.ok(name+id);
    }

執(zhí)行結(jié)果:

3.2 單個參數(shù)

    @PostMapping("/test4")
    public Result test4(@RequestParam("name") String name) {
        return Result.ok(name);
    }

執(zhí)行結(jié)果:

四、@PathVariable

   @PathVariable 用于綁定 url 中的占位符。例如:請求 url 中 /delete/{id},這個{id}就是 url 占位符。url 支持占位符是 spring3.0 之后加入的。是 springmvc 支持 rest 風(fēng)格 URL 的一個重要標志。

4.1  單個參數(shù)

    @PostMapping("/test2/{id}")
    public Result test2(@PathVariable("id") Integer id) {
        return Result.ok(id);
    }

4.2  多個參數(shù)

  @GetMapping("/test1/{id}/{name}")
    public Result test1(@PathVariable("id") Integer id,
                        @PathVariable("name") String name) {
        return Result.ok(id+":"+name);
    }

五、@RequestBody

@RequestBody一般被用來接收http請求中body中json數(shù)據(jù)。get、post都可以使用。一般用于post。

5.1  單個參數(shù)

注意:不支持 (@RequestBody String name 2,@RequestBody String name2)

   @PostMapping("/test5")
    public Result test5(@RequestBody String name) {
        return Result.ok(name);
    }

不同傳參得到的結(jié)果不同:

" 李四 "

JSON格式:

{
  "name": "李四"
}

5.2  User對象

    @PostMapping("/test6")
    public Result test6(@RequestBody User user) {
        return Result.ok(user);
    }

 結(jié)果:

5.3  Map對象 

    @PostMapping("/test7")
    public Result test7(@RequestBody HashMap map) {
        return Result.ok(map);
    }

結(jié)果:

5.4  List 集合

    @PostMapping("/test8")
    public Result test8(@RequestBody List<User> list) {
        return Result.ok(list);
    }

結(jié)果:

六:RequestHeader

@RequestHeader主要用來獲取請求當中的請求頭

代碼示例:

    @PostMapping("/test9")
    public Result test9(@RequestHeader("token") String token ) {
        return Result.ok(token);
    }

結(jié)果:

七、HttpServletRequest

直接拿到request對象,通過request可以從對象中靈活的獲取參數(shù):

@RestController
@RequestMapping("/request")
public class HttpServletRequestController {
 
    @GetMapping("/getUrlValue")
    public String getUrlValue(HttpServletRequest request) {
        // 沒有的時候不會報錯,直接為null
        String msg = request.getParameter("msg");
        System.out.println(msg);
        return msg;
    }
 
    @GetMapping("/getUrlValues")
    public String getHttpServletRequestValue(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        return JSONObject.toJSONString(request.getParameterMap());;
    }
}

總結(jié)

到此這篇關(guān)于Spring Boot中優(yōu)雅地處理參數(shù)傳遞的技巧的文章就介紹到這了,更多相關(guān)SpringBoot處理參數(shù)傳遞技巧內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時候報decompiled.class file bytecode version:52.0(java 8)錯誤的解決辦法

    IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時候報decompiled.class file byt

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時候報decompiled.class file bytecode version:52.0(java 8)錯誤的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 最新評論