- @GetMapping("/test")
- public Map test(@RequestParam("id")Integer id,
- @RequestParam("username")String name,
- @RequestParam("interests") List
interests, - @PathVariable Map
param_map){ - Map
map = new HashMap<>(); - map.put("id",id);
- map.put("name",name);
- map.put("interests",interests);
- map.put("param_map",param_map);
- return map;
- }
非特别说明,都是在@RestController中进行测试
里面的username和name可以相同也可以不同
可以定义一个Map
页面测试路径:/test?id=1&username=root&interests=tv&interests=game
或 /test?id=1&username=root&interests=tv,game
- @GetMapping("/test/{id}/{name}")
- public Map test(@PathVariable("id")Integer id,
- @PathVariable("name")String name,
- @PathVariable Map
param_map){ - Map
map = new HashMap<>(); - map.put("id",id);
- map.put("name",name);
- map.put("param_map",param_map);
- return map;
- }
页面测试路径:/test/1/root
- @GetMapping("/test")
- public Map test(@RequestHeader("User_Agent")String userAgent,
- @RequestHeader Map
param_map){ - Map
map = new HashMap<>(); - map.put("userAgent",userAgent);
- map.put("param_map",param_map);
- return map;
- }
页面测试路径:/test
- @GetMapping("/test")
- public Map test(@CookieValue("_ga") String _ga,
- @CookieValue("_ga") Cookie cookie){
- Map
map = new HashMap<>(); - map.put("_ga",_ga);
- map.put("cookie",cookie);
- return map;
- }
页面测试路径:/test
- @PostMapping("/save")
- public Map test(@RequestBody String content){
- Map
map = new HashMap<>(); - map.put("content",content);
- return map;
- }
注意这个是post请求获取请求内容信息,获取到的格式为:userName=root&email=root@qq.com
- @Controller
- public class TestController {
- @GetMapping("/goto")
- public String goToPage(HttpServletRequest request){
- request.setAttribute("msg","goto参数");
- return "forward:/success"; //请求转发到 /success请求
- }
- @GetMapping("/success")
- public Map success(@RequestAttribute String msg,HttpServletRequest request){
- Object msg1 = request.getAttribute("msg");
- Map
map = new HashMap<>(); - map.put("msg1",msg1);
- map.put("msg",msg);
- return map;
- }
- }
页面测试路径:/goto,页面路径不变,实际会跳转到/success中执行,也可写成
- request.getRequestDispatcher("/index").forward(request, response);
- //或者
- return "forward:/index";
redirect则是直接重定向到新路径,页面路径会变,也可写成
- response.sendRedirect("/index");
- //或者
- return "redirect:/index";
post请求要跳转必须使用redirect,防止路径不变导致刷新之后表单重新提交,但使用这个,就无法通过request传递参数,但可以使用RedirectAttributes的addFlashAttribute方法传递参数
注意,springboot默认不开启矩阵变量识别,需要自己手动开启
- //第一种开启方法,在config类中
- @Configuration(proxyBeanMethods = false)
- public class WebConfig implements WebMvcConfigurer {
- @Override
- public void configurePathMatch(PathMatchConfigurer configurer) {
- UrlPathHelper urlPathHelper = new UrlPathHelper();
- //不移除url路径分号;后面的内容,矩阵变量功能才可以生效
- urlPathHelper.setRemoveSemicolonContent(false);
- configurer.setUrlPathHelper(urlPathHelper);
- }
- }
- //第二种开启方法
- @Configuration(proxyBeanMethods = false)
- public class WebConfig{
- @Bean
- public WebMvcConfigurer webMvcConfigurer() {
- return new WebMvcConfigurer(){
- @Override
- public void configurePathMatch(PathMatchConfigurer configurer) {
- UrlPathHelper urlPathHelper = new UrlPathHelper();
- //不移除url路径分号;后面的内容,矩阵变量功能才可以生效
- urlPathHelper.setRemoveSemicolonContent(false);
- configurer.setUrlPathHelper(urlPathHelper);
- }
- }
- }
- }
一层的情况:
- @GetMapping("/test/{name}")
- public Map test(@PathVariable String name,
- @MatrixVariable("age") Integer age){
- Map
map = new HashMap<>(); - map.put("name",name);
- map.put("age",age);
- return map;
- }
页面测试路径:/test/root;age=20
多层的情况:
- @GetMapping("/test/{bossId}/{empId}")
- public Map test(@PathVariable String bossId,
- @PathVariable String empId,
- @MatrixVariable(value = "age",pathVar = "boosId") Integer boss_age,
- @MatrixVariable(value = "age",pathVar = "empId") Integer emp_age){
- Map
map = new HashMap<>(); - map.put("bossId",bossId);
- map.put("empId",empId);
- map.put("boss_age",boss_age);
- map.put("emp_age",emp_age);
- return map;
- }
使用 pathVar 区分是哪个路径变量带的参
页面测试路径:/test/1;age=20/4;age=25
需要在配置类中设置

这样在controller里的方法,就可以通过传进来的“阿猫,3”这种参数,直接获取到猫这个类的实例对象了。