SpringMVC 路由约束
.NET Core 中的概念
无聊逛论坛 突然看到了个 路由约束
好像没听过 搜索了一下 原来是 .NET Core 中的概念 文档
[GET] /api/posts/{id}
[GET] /api/posts/{name}
这样两个地址 可以通过 路由约束 分别映射到不同的地址
.NET Core内置的路由约束
检查数据类型的约束
检查数据的值/长度/范围的约束
正则表达式约束检查数据类型的约束
约束 行内 Constraint类 说明 int {id:int}IntRouteConstraint只允许int32整数 alpha {id:alpha}AlphaRouteConstraint只能包含大小写字母 bool {id:bool}BoolRouteConstraint只允许布尔类型 datetime {id:datetime}DateTimeRouteConstraint只允许日期格式 decimal {id:decimal}DecimalRouteConstraint只允许decimal类型 double {id:double}DoubleRouteConstraint只允许double类型 float {id:float}FloatRouteConstraint只允许float类型 guid {id:guid}GuidRouteConstraint只允许guid类型 检查数据的值/长度/范围的约束
约束 行内 Constraint类 说明 length(length) {id:length(12)}LengthRouteConstraint字符串长度限制 maxlength(value) {id:maxlength(8)}MaxLengthRouteConstraint字符串最大长度限制 minlength(value) {id:minlength(4)}MinLengthRouteConstraint字符串最小长度限制 range(min,max) {id:range(18,120)}RangeRouteConstraint数值范围限制 min(value) {id:min(18)}MinRouteConstraint最小数值限制 max(value) {id:max(120)}MaxRouteConstraint最大数值限制 正则表达式约束
约束 行内 Constraint类 说明 regex(expression) {ssn:regex(^\d{{3}}-\d{{2}}-\d{{4}}$)}/RegexRouteConstraint正则表达式约束
Java SpringMVC 对应实现
URI Patterns
一些示例:
"/resources/ima?e.png"- match one character in a path 匹配路径段中的一个字符segment"/resources/*.png"- match zero or more characters in a path segment 匹配路径段中的零个或多个字符"/resources/**"- match multiple path segments 匹配多个路径段"/projects/{project}/versions"- match a path segment and capture it as a variable 匹配路径段并将其捕获为变量"/projects/{project:[a-z]+}/versions"- match and capture a variable with a regex 使用正则表达式匹配并捕获变量
1 |
|