首页 > 解决方案 > 春季启动 2.2.5。如何在 Spring Filter 中检索 PathVariable 参数

问题描述

我想解决的问题。我需要将特定逻辑应用于 url 属于特定子路径的所有 restful 端点:比如说“/api/employee/{id}”。这意味着以该路径开头的所有链接都应应用基于员工 ID 的逻辑,我试图直接在 Spring Boot 过滤器中应用该逻辑,以避免将逻辑传播到任何地方。

我面临的问题。我能够从 ServletRequest 中获取查询参数,但过滤器中没有 PathVariables。

知道如何解析吗?

将不胜感激:)

标签: spring-bootfilterrestful-urlpath-variables

解决方案


PathVariables 只是 URI。你不能打电话getRequestURI()

从文档:

java.lang.String getRequestURI()

Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:

First line of HTTP request  Returned Value

POST /some/path.html HTTP/1.1       /some/path.html

GET http://foo.bar/a.html HTTP/1.0      /a.html

HEAD /xyz?a=b HTTP/1.1      /xyz

https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getRequestURI--


推荐阅读