Springboot使用MatrixVariable 注解

    根据 URI 规范 RFC 3986 中 URL 的定义,路径片段中可以可以包含键值对。规范中没对对应的术语。一般 “URL 路径参数” 可以被应用,尽管更加独特的 “矩阵 URI” 也经常被使用并且相当有名。在 Spring MVC 它被成为矩阵变量

    矩阵变量可以出现在任何路径片段中,每一个矩阵变量都用分号(;)隔开。比如 “/cars;color=red;year=2012”。多个值可以用逗号隔开,比如 “color=red,green,blue”,或者分开写 “color=red;color=green;color=blue”。

    如果你希望一个 URL 包含矩阵变量,那么请求映射模式必须用 URI 模板来表示这些矩阵变量。这样的话,不管矩阵变量顺序如何,都能够保证请求可以正确的匹配。

Springboot 默认是无法使用矩阵变量绑定参数的。需要覆盖WebMvcConfigurer中的configurePathMatch方法。

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper=new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

基于XML的配置

<mvc:annotation-driven enable-matrix-variables="true" />

编写矩阵变量控制器

package com.techmap.examples.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/matrix")
public class MatrixController
{

    /**
     * 使用矩阵变量
     */
    @GetMapping("/owners/{ownerId}/pets/{petId}")
    public String findPet(
            @PathVariable String ownerId,
            @PathVariable String petId,
            @MatrixVariable(name = "q", pathVar = "ownerId") int q1,
            @MatrixVariable(name = "q", pathVar = "petId") int q2)
    {
        System.out.println("--> ownerId : " + ownerId);
        System.out.println("--> petId : " + petId);
        System.out.println("--> q1 : " + q1);
        System.out.println("--> q2 : " + q2);

        return "/examples/targets/test1";
    }

    /**
     * 矩阵变量可以设置默认值
     */
    @GetMapping("/pets/{petId}")
    public String findPet(
            @MatrixVariable(required = false, defaultValue = "1") int q)
    {
        System.out.println("--> Default value of q : " + q);

        return "/examples/targets/test2";
    }
}

原文地址:https://www.cnblogs.com/deityjian/p/11621143.html

时间: 2024-08-30 14:26:17

Springboot使用MatrixVariable 注解的相关文章

springboot 定时任务@Scheduled注解

需要定时器的地方好像还挺多. 之前项目中有用到使用定时器循环订单时间,然后将超时的订单状态更改. springboot的@Scheduled注解能够很快速完成我们需要的定时任务. @Component public class ExampleTimer { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); /*每100秒执行一次*/ @Scheduled(fixedRate = 100000) public

SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地址信息address,每个用户可以有多量车car,类似如下结构 |-- user |-- address |-- carList |-- car1 |-- car2 二.对应的实体类如下 /省略setter/getter public class Address { private Long id;

springboot + shiro 权限注解、请求乱码解决、统一异常处理

springboot + shiro 权限注解.请求乱码解决.统一异常处理 前篇 后台权限管理系统 相关: spring boot + mybatis + layui + shiro后台权限管理系统 springboot + shiro之登录人数限制.登录判断重定向.session时间设置 springboot + shiro 动态更新用户信息 基于前篇,新增功能: 新增shiro权限注解: 请求乱码问题解决: 统一异常处理. 源码已集成到项目中: github源码: https://githu

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht

SpringBoot利用自定义注解实现AOP

SpringBoot利用自定义注解实现AOP java 本文主要讲解利用SpringBoot的自定义注解来实现AOP思想. 在开始所有的操作之前,需要导入aop坐标: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 如何自定义注解? 实际上注解本

SpringBoot(15)—@Conditional注解

SpringBoot(15)-@Conditional注解 作用 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件的才给容器注册Bean. 一.概述 1.@Conditional注解定义 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { Clas

springboot使用@data注解

springboot使用@data注解,减少不必要代码原创不爱编程的设计师 发布于2018-10-22 16:41:28 阅读数 46839 收藏展开 一.idea安装lombok插件 二.重启idea三.添加maven依赖<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</vers

springboot使用ImportResource注解加载spring配置文件(传智播客代码)

接上篇:springboot使用PropertyResource注解读取指定配置文件的属性(传智播客代码)@ImportResource可以加载多个配置文件 DemoApplication.java package com.atguigu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import or

SpringBoot的核心注解和配置

一.入口类和SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法. @SpringBootApplication注解是Spring Boot的核心注解,它是一个组合注解: @SpringBootConfiguration:这是Spring Boot项目的配置注解,这也是一个组合注解,与@Configuration作用相同,标识这是一个被装载的Bean,在Spring