java - Spring MVC uri 与百分比编码字符的映射

标签 java spring spring-mvc encoding

我有一个在 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/

相关文章:

java - Genymotion 与不同本地子网中的套接字通信

java - 我有一个特定时间作为字符串 ("7:00 PM ET")。 Joda-Time 可以将该字符串转换为用户的时区吗?

java - 在 Java/Spring 中处理与数据库相关的异常

java - Redis:在关闭其中一个 Redis 主节点时,Spring Boot 应用程序请求一直失败

java - RichFaces ajax commandButton 问题

java - 如何以编程方式创建其中包含 EditText 的选项卡?

jquery - 针对特殊要求的 JSF 2.0 与 Wicket 与 SpringMVC 3.x

java - 我的 thymeleaf html 表单有什么问题吗?为什么密码显示值为空?

java - 如何在 Spring Boot 中处理最大文件大小异常?

spring-mvc - 使用 HandlerInterceptor 通过 Spring Web Flow 添加模型属性