SpringBoot请求处理-常用参数使用 @RequestAttribute、@RequestParam、@RequestHeader 、@PathVariable、@MatrixVariable

SpringBoot请求处理-常用参数使用 @RequestAttribute、@RequestParam、@RequestHeader 、@PathVariable、@MatrixVariable,第1张

SpringBoot请求处理-常用参数使用 @RequestAttribute、@RequestParam、@RequestHeader 、@PathVariable、@MatrixVariable

文章目录

一.常用参数注解使用

1. @PathVariable 路径变量2.@RequestHeader 获取请求头3.@RequestParam 获取请求参数4.@cookievalue 获取cookie值5.@RequestBody 获取请求体[POST]6.请求处理-@RequestAttribute7.@MatrixVariable与UrlPathHelper


一.常用参数注解使用
@RestController
public class HelloController {

    @RequestMapping("/a.jpg")
    public String hello(@RequestParam("username") String name){//请求参数username给name
//        HttpSession session *** 作session
//        Model model  放request域
//        Person person 自定义类型请求对象        
        return "aaaaaa";
    }
}
1. @PathVariable 路径变量

@PathVariable 路径变量

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class ParameterTestController {
    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map pv
    ){
        Map map=new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        return map;
    }
}
2.@RequestHeader 获取请求头
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class ParameterTestController {
    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map pv,
                                     @RequestHeader("user-Agent") String userAgent,
                                     @RequestHeader Map header
    ){
        Map map=new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        map.put("user-Agent",userAgent);
        map.put("headers",header);
        return map;
    }
}

3.@RequestParam 获取请求参数

(指问号后的参数,url?a=1&b=2)

@RestController
public class ParameterTestController {
    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map pv,
                                     @RequestHeader("user-Agent") String userAgent,
                                     @RequestHeader Map header,
                                     @RequestParam("age") Integer age,
                                     @RequestParam("interes") List interes,
                                     @RequestParam Map params
    ){
        Map map=new HashMap<>();
//        map.put("id",id);
//        map.put("name",name);
//        map.put("pv",pv);
//        map.put("user-Agent",userAgent);
//        map.put("headers",header);
        map.put("age",age);
        map.put("interes",interes);
        map.put("params",params);
        return map;
    }
}

4.@cookievalue 获取cookie值
@RestController
public class ParameterTestController {
    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map pv,
                                     @RequestHeader("user-Agent") String userAgent,
                                     @RequestHeader Map header,
                                     @RequestParam("age") Integer age,
                                     @RequestParam("interes") List interes,
                                     @RequestParam Map params,
                                     @cookievalue("_ga")  String _ga,
                                     @cookievalue("_ga") cookie cookie
    ){
        Map map=new HashMap<>();
//        map.put("id",id);
//        map.put("name",name);
//        map.put("pv",pv);
//        map.put("user-Agent",userAgent);
//        map.put("headers",header);
        map.put("age",age);
        map.put("interes",interes);
        map.put("params",params);
        map.put("_ga",_ga);
        System.out.println(cookie.getName()+"===>"+cookie.getValue());
        return map;
    }
}
5.@RequestBody 获取请求体[POST]
    @PostMapping("/save")
    public Map postMethod(@RequestBody String content){
        Map map = new HashMap<>();
        map.put("content",content);
        return map;
    }
6.请求处理-@RequestAttribute

获取request域属性

@Controller
public class RequestController {

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){

        request.setAttribute("msg","成功了...");
        request.setAttribute("code",200);
        return "forward:/success";  //转发到  /success请求
    }
    
    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,
                       @RequestAttribute(value = "code",required = false)Integer code,
                       HttpServletRequest request){
        Object msg1 = request.getAttribute("msg");
        Map map = new HashMap<>();
        map.put("reqMethod_msg",msg1);
        map.put("annotation_msg",msg);
        return map;
    }
}

7.@MatrixVariable与UrlPathHelper

/cars/sell;low=34;brand=byd,audi,yd 矩阵变量
例:cars/sell;low=33;brand=byd,bb.ik
url重写:/abc;jsessionid=xxxx 把cookie的值使用矩阵变量的方式进行传递

    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List brand,
                        @PathVariable("path") String path){
        Map map = new HashMap<>();

        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

SpringBoot默认禁用矩阵变量
手动开启:对于路径的处理使用UrlPathHelper进行解析
removeSemicolonContent(移除分号内容)支持矩阵变量的

实现WebMvcConfigurer接口:

@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

        UrlPathHelper urlPathHelper = new UrlPathHelper();
        // 不移除;后面的内容。矩阵变量功能就可以生效
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

创建返回WebMvcConfigurerBean:

@Configuration(proxyBeanMethods = false)
public class WebConfig{
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
                        @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                // 不移除;后面的内容。矩阵变量功能就可以生效
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        }
    }
}

@MatrixVariable的用例

    // /boss/1;age=20/2;age=10

    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        Map map = new HashMap<>();

        map.put("bossAge",bossAge);
        map.put("empAge",empAge);
        return map;

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存