java - 使用 JAX-RS 的不区分大小写的 URL

标签 java rest jersey jax-rs

有什么简单的方法可以在 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/

相关文章:

java - IntelliJ IDEA 如何删除感叹号后的空格

java - 在eclipse上导入现有项目后突然报错ObjectSetting.java文件

java - 使用单个元素区分 String 列表和 String[] 列表

c# - Rest API 设计(twitter)

objective-c - iOS 应用程序运行时禁用闲置模式

java - 什么是泽西过滤器?

xml - 调用 Jersey api 时无法指定接受哪种响应类型

java - JD-GUI反编译后源码有注释

rest - 当我尝试使用 Instagram API 连接到 oauth2.0 时,我从 api.instagram.com 返回了 400 - Bad Result。为什么?

java - 在 Java Restful API JSON 结果字符串上获取意外的额外转义字符