天天看点

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开发(请求参数)

返回值处理器