在Akka HTTP中路由DSL与正则表达式不正确匹配。 我正在使用Akka HTTP版本10.8.1。
我正在按照以下文档中的示例进行操作 https://doc.akka.io/docs/akka-http/current/routing-dsl/path-matchers.html,例如:
// matches e.g. /foo/bar123 and extracts "123" as a String
path("foo" / """bar(\d+)""".r)
这里是我所拥有的:
2条路径和2条路线
roles/members/01cb21e8-7368-4825-856f-c87b44b8c9a0
path("roles" / "members" / s"($uuidRegex)".r) { token =>
roles/permissions/RoleUuid:01cb21e8-7368-4825-856f-c87b44b8c9a0
path("roles" / "permissions" / s"RoleUuid:($uuidRegex)".r) { token =>
在处理程序中:
val uuidRegex = """[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"""
override val routes: Route = extractRequestContext { implicit rc =>
path("roles" / "members" / s"($uuidRegex)".r) { token =>
get { rc =>
val method = "get role members"
logger.info(s"RoleUUID: method(RoleUUID): $method")
logger.info(s"RoleUUID: token: $token")
val roleUuidOpt = Try {
logger.info(s"RoleUUID: stringToId: converting $token")
RoleUuid(UUID.fromString(token))
}
logger.info(s"RoleUUID: roleUuid: $roleUuidOpt")
rolesOps(rc)
.getMembers(roleUuidOpt.get)
.map(x => completeStr(x.asJson.noSpaces))
}
} ~
path("roles" / "permissions" / s"RoleUuid:($uuidRegex)".r) { token =>
get { rc =>
val method = "get role permissions"
logger.info(s"RoleUUID: method(RoleUUID): $method")
logger.info(s"RoleUUID: token: $token")
val roleUuidOpt = Try {
logger.info(s"RoleUUID: stringToId: converting $token")
RoleUuid(UUID.fromString(token))
}
logger.info(s"RoleUUID: roleUuid: $roleUuidOpt")
rolesOps(rc)
.getRolePermissions(roleUuidOpt.get)
.map(x => completeStr(x.asJson.noSpaces))
}
}
}
第一个路径(只有uuid)
roles/members/01cb21e8-7368-4825-856f-c87b44b8c9a0
有效,就是匹配了,我看到了日志语句
第二条路径(带有标识符和uuid) 角色/权限/ RoleUuid:01cb21e8-7368-4825-856f-c87b44b8c9a0
不起作用,它不匹配,我看不到日志语句 因为我没有看到第一条日志语句,所以我假设它从未匹配
有效路径与无效路径之间的唯一区别是路径匹配器中的前缀。
答案 0 :(得分:1)
使用Scala's String interpolation(注意最后一个路径段前面的s
和$
前面的uuidRegex
):
val uuidRegex = """[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"""
path("roles" / "members" / s"RoleId:($uuidRegex)".r) {
// ...
}
例如,上面的路径匹配对以下内容的调用:
http://yourhost:yourport/roles/members/RoleId:d11ffab5-ab40-4e55-9e3e-621df1992470