天天看點

springboot-web開發(請求參數)

注解

@PathVariable, @RequestHeader,@ModelAttribute,@RequestParam,@MatrixVariable,@CookieValue,@ResponseBody

springboot-web開發(請求參數)
<html>

<head>
    <meta charset="UTF-8">
</head>

<body>

<ul>
    <a href="car/3/owner/lisi?age=18&interests=basketball&interests=game">car/{id}/owner/{username}</a>
    <li>@PathVariable(路徑變量)</li>
    <li>@RequestHeader(擷取請求頭)</li>
    <li>@RequestParam(請求參數)</li>
    <li>@CookieValue(擷取Cookie值)</li>
    <li>@RequestBody(擷取請求體[POST])</li>
</ul>

    <form action="/save" method="POST">
        測試@RequestBody擷取資料<br/>
        使用者名:<input name="username"/><br/>
        郵箱:  <input name="email"/></br>
        <input type="submit" value="送出">
    </form>

<a href="/cars/sell;low=24;brand=byd,audi,yd">@MatrixVariable(矩陣變量)</a><br/>
<a href="/cars/sell;low=24;brand=byd;brand=audi;brand=yd">@MatrixVariable(矩陣變量)</a><br/>
<a href="/boss/1;age=20/2;age=10">@MatrixVariable(矩陣變量) /boss/{bossId}/{empId}</a><br/>

</body>

</html>           
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParameterTestController {

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String name,
                                      @PathVariable Map<String, String> pv,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> heads,
                                      @RequestParam("age") Integer age,
                                      @RequestParam("interests") List<String> interests,
                                      @RequestParam Map<String, String> params
                                      //@CookieValue("_ga") Cookie cookie
                                      ){
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        map.put("userAgent",userAgent);
        map.put("heads",heads);
        map.put("age",age);
        map.put("interests",interests);
        map.put("params",params);
        //map.put("Cookie",cookie);
        return map;
    }

    @PostMapping("/save")
    public Map<String, Object> postMethod(@RequestBody String content){
        Map<String, Object> map = new HashMap<>();
        map.put("content",content);
        return map;
    }

    /*
    * spring boot 預設禁用了矩陣變量的功能
    *   手動開啟:原理. 對于路徑的處理. UrlPathHelper類進行解析
    *               removeSemicolonContent(移除分号内容)支援矩陣變量
    */
    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") String low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        Map<String, Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bage,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer eage,
                        @PathVariable("bossId") String bossId,
                        @PathVariable("empId") String empId){
        Map<String, Object> map = new HashMap<>();
        map.put("bage",bage);
        map.put("eage",eage);
        map.put("bossId",bossId);
        map.put("empId",empId);
        return map;
    }
}
           

矩陣變量的使用

springboot-web開發(請求參數)

 Servlet API

springboot-web開發(請求參數)

複雜參數 

springboot-web開發(請求參數)

自定義對象參數 

springboot-web開發(請求參數)

1. handlerMapping中找到能處理請求的Handler

2. 為目前handler找一個擴充卡HandlerAdapter

springboot-web開發(請求參數)

參數解析器

 确定将要執行的目标方法的每一個參數的值是什麼

springboot-web開發(請求參數)
springboot-web開發(請求參數)

傳回值處理器