我有一个在 Tomcat 7 上运行的 spring mvc 应用程序,其 http 和 ajp 连接器配置为 URIEncoding="UTF-8"。 我的 REST Controller 片段:
@Controller
@RequestMapping("")
public class ClientRestApi {
private final String API_PREFIX = "/api/1.0/client";
...
@RequestMapping(value = API_PREFIX + "/{clientId}", method = RequestMethod.GET)
@ResponseBody
public ClientDetails get(@PathVariable String clientId, HttpServletRequest request) {
log.info("Client API GET [" + clientId + "] | " + request.getRequestURI());
...
}
}
- 当我访问:
http://www.example.pl/api/1.0/client/abc
时,我得到 abc 客户端页面 - 正确 - 当我访问:
http://www.example.pl/%07api/1.0/client/abc
时,我得到 abc 客户端页面 - 错误 - 当我访问:
http://www.example.pl/%0bapi/1.0/client/abc
时,我得到 abc 客户端页面 - 错误 - 当我访问:
http://www.example.pl/ap%0bi/1.0/client/abc
时,我得到 http 404 - 正确
在应用程序日志中我可以看到(前 3 个请求):
ClientRestApi - Client API GET [abc] | /api/1.0/client/abc
ClientRestApi - Client API GET [abc] | /%07api/1.0/client/abc
ClientRestApi - Client API GET [abc] | /%0bapi/1.0/client/abc
我的问题是为什么我的错误示例是错误的? 为什么它们不是 http 404?
在应用程序的 web.xml 文件中,我有带有 UTF-8 编码的 CharacterEncodingFilter 过滤器。我的应用程序中从未遇到过编码错误的问题。
编辑: 从扩展日志中,请求 http://www.example.pl/%0bapi/1.0/client/abc给出:
DEBUG RequestMappingHandlerMapping - Looking up handler method for path /^Kapi/1.0/client/abc
TRACE RequestMappingHandlerMapping - Found 1 matching mapping(s) for [/^Kapi/1.0/client/abc] : [{[/api/1.0/client/{
clientId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}]
DEBUG RequestMappingHandlerMapping - Returning handler method [public ClientDetails ...ClientRestApi.get(java.lang.String,javax.servlet.http.HttpServletRequest)]
最佳答案
AntPathMatcher 标记路径并默认修剪所有段(请参阅 String.trim 的 Javadoc)。这种行为是可以控制的。您可以使用带有 setTrimTokens(false) 的 AntPathMatcher 配置 RequestMappingHandlerMapping。
关于java - Spring MVC uri 与百分比编码字符的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26110136/