• spring Controller参数


    1、@RequestParam 请求参数

     

    1. @GetMapping("/test")
    2. public Map test(@RequestParam("id")Integer id,
    3. @RequestParam("username")String name,
    4. @RequestParam("interests") List interests,
    5. @PathVariable Map param_map){
    6. Map map = new HashMap<>();
    7. map.put("id",id);
    8. map.put("name",name);
    9. map.put("interests",interests);
    10. map.put("param_map",param_map);
    11. return map;
    12. }

    非特别说明,都是在@RestController中进行测试

    里面的username和name可以相同也可以不同

    可以定义一个Map获取全部参数(后面出现的全部是这个情况,不再说明)

    页面测试路径:/test?id=1&username=root&interests=tv&interests=game

    或 /test?id=1&username=root&interests=tv,game

    2、@PathVariable 路径变量

    1. @GetMapping("/test/{id}/{name}")
    2. public Map test(@PathVariable("id")Integer id,
    3. @PathVariable("name")String name,
    4. @PathVariable Map param_map){
    5. Map map = new HashMap<>();
    6. map.put("id",id);
    7. map.put("name",name);
    8. map.put("param_map",param_map);
    9. return map;
    10. }

    页面测试路径:/test/1/root

    3、@RequestHeader 请求头数据

    1. @GetMapping("/test")
    2. public Map test(@RequestHeader("User_Agent")String userAgent,
    3. @RequestHeader Map param_map){
    4. Map map = new HashMap<>();
    5. map.put("userAgent",userAgent);
    6. map.put("param_map",param_map);
    7. return map;
    8. }

    页面测试路径:/test

    4、@CookieValue Cookie的值

    1. @GetMapping("/test")
    2. public Map test(@CookieValue("_ga") String _ga,
    3. @CookieValue("_ga") Cookie cookie){
    4. Map map = new HashMap<>();
    5. map.put("_ga",_ga);
    6. map.put("cookie",cookie);
    7. return map;
    8. }

    页面测试路径:/test

    5、@RequestBody 获取表单内容信息

    1. @PostMapping("/save")
    2. public Map test(@RequestBody String content){
    3. Map map = new HashMap<>();
    4. map.put("content",content);
    5. return map;
    6. }

    注意这个是post请求获取请求内容信息,获取到的格式为:userName=root&email=root@qq.com

    6、@RequestAttribute 获取在请求中设置的属性

    1. @Controller
    2. public class TestController {
    3. @GetMapping("/goto")
    4. public String goToPage(HttpServletRequest request){
    5. request.setAttribute("msg","goto参数");
    6. return "forward:/success"; //请求转发到 /success请求
    7. }
    8. @GetMapping("/success")
    9. public Map success(@RequestAttribute String msg,HttpServletRequest request){
    10. Object msg1 = request.getAttribute("msg");
    11. Map map = new HashMap<>();
    12. map.put("msg1",msg1);
    13. map.put("msg",msg);
    14. return map;
    15. }
    16. }

    页面测试路径:/goto,页面路径不变,实际会跳转到/success中执行,也可写成

    1. request.getRequestDispatcher("/index").forward(request, response);
    2. //或者
    3. return "forward:/index";

    redirect则是直接重定向到新路径,页面路径会变,也可写成

    1. response.sendRedirect("/index");
    2. //或者
    3. return "redirect:/index";

    post请求要跳转必须使用redirect,防止路径不变导致刷新之后表单重新提交,但使用这个,就无法通过request传递参数,但可以使用RedirectAttributes的addFlashAttribute方法传递参数

    7、@MatrixVariable 矩阵变量(较少使用)

    注意,springboot默认不开启矩阵变量识别,需要自己手动开启

    1. //第一种开启方法,在config类中
    2. @Configuration(proxyBeanMethods = false)
    3. public class WebConfig implements WebMvcConfigurer {
    4. @Override
    5. public void configurePathMatch(PathMatchConfigurer configurer) {
    6. UrlPathHelper urlPathHelper = new UrlPathHelper();
    7. //不移除url路径分号;后面的内容,矩阵变量功能才可以生效
    8. urlPathHelper.setRemoveSemicolonContent(false);
    9. configurer.setUrlPathHelper(urlPathHelper);
    10. }
    11. }
    12. //第二种开启方法
    13. @Configuration(proxyBeanMethods = false)
    14. public class WebConfig{
    15. @Bean
    16. public WebMvcConfigurer webMvcConfigurer() {
    17. return new WebMvcConfigurer(){
    18. @Override
    19. public void configurePathMatch(PathMatchConfigurer configurer) {
    20. UrlPathHelper urlPathHelper = new UrlPathHelper();
    21. //不移除url路径分号;后面的内容,矩阵变量功能才可以生效
    22. urlPathHelper.setRemoveSemicolonContent(false);
    23. configurer.setUrlPathHelper(urlPathHelper);
    24. }
    25. }
    26. }
    27. }

    一层的情况:

    1. @GetMapping("/test/{name}")
    2. public Map test(@PathVariable String name,
    3. @MatrixVariable("age") Integer age){
    4. Map map = new HashMap<>();
    5. map.put("name",name);
    6. map.put("age",age);
    7. return map;
    8. }

    页面测试路径:/test/root;age=20

    多层的情况:

    1. @GetMapping("/test/{bossId}/{empId}")
    2. public Map test(@PathVariable String bossId,
    3. @PathVariable String empId,
    4. @MatrixVariable(value = "age",pathVar = "boosId") Integer boss_age,
    5. @MatrixVariable(value = "age",pathVar = "empId") Integer emp_age){
    6. Map map = new HashMap<>();
    7. map.put("bossId",bossId);
    8. map.put("empId",empId);
    9. map.put("boss_age",boss_age);
    10. map.put("emp_age",emp_age);
    11. return map;
    12. }

    使用 pathVar 区分是哪个路径变量带的参

    页面测试路径:/test/1;age=20/4;age=25

    8、获取参数时自定义Convert数据转换器

    需要在配置类中设置

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

  • 相关阅读:
    Redis之分布式锁
    支付交易-交易分发器(二)
    执行Hive查询时出现OOM
    性能测试工具Jmeter你所不知道的东西····
    开源模型应用落地-安全合规篇(一)
    java之多线程
    线段树——维护序列(两个懒标记的情况)
    Flink系列文档-(YY05)-Flink编程API-多流算子
    分布式IT监控系统
    初识Layering Sequence
  • 原文地址:https://blog.csdn.net/liangqibinde/article/details/127387383