有什么简单的方法可以在 JAX-RS 网络服务中提供不区分大小写的 URL 吗?这样做的目标是生成一个网络服务,它是一个“宽松的接受者”。1
我想可以用一个过滤器来做到这一点,该过滤器 .to[Lower|Upper]Case()
所有传入的 URL。不幸的是,此实现要求程序员在确保应用程序中的所有硬编码 URL 字符串严格为 [小写|大写] 时保持纪律/一致性。
另外,我还不知道 JAX-RS 类似于 servlet 过滤器。
如果重要的话,我使用 Jersey 作为我的 JAX-RS 实现。
1如,“宽于所接受,严于所出”(记不起出处)
最佳答案
答案是否定的,因为基本上 URI 根据 RFC 3986区分大小写:
6.2.2.1. Case Normalization
For all URIs, the hexadecimal digits within a percent-encoding triplet (e.g., "%3a" versus "%3A") are case-insensitive and therefore should be normalized to use uppercase letters for the digits A-F.
When a URI uses components of the generic syntax, the component syntax equivalence rules always apply; namely, that the scheme and host are case-insensitive and therefore should be normalized to lowercase. For example, the URI is equivalent to http://www.example.com/. The other generic syntax components are assumed to be case-sensitive unless specifically defined otherwise by the scheme (see Section 6.2.3).
如果你仍然想让它们不区分大小写,你可以使用一个 servlet 过滤器并将它放在 JAX-RS 框架的前面。您仍然需要在应用程序中保持一致。
如果您考虑从 Jersey 切换到 Apache Wink , 您可以使用 Dynamic Resources以确保所有网址都是小写/大写。因此,将 servlet 过滤器与动态资源相结合可以成为这种情况的完整解决方案。
关于java - 使用 JAX-RS 的不区分大小写的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624481/