为什么 Jersey 的 UriBuilder.build 方法编码 #'s 和 %'s,而不是 /'s?

发布于 2024-11-14 20:56:53 字数 1928 浏览 5 评论 0原文

我有一个相当典型的 REST API,只是资源的 id 不是整数,而是字符串,通常包含 / 字符。因此,如果客户的 ID 为 string/with/slashes,则该客户的 URI 应为 http://localhost/customers/string%2Fwith%2Fslashes。返回客户列表时,我想使用 UriBuilder 这样我就可以将它放在 ATOM 样式链接元素的 href 中。但这并不完全有效;这是一个小测试类,它显示了我的意思:

@Path("/customers")
public class JerseyTest {

  @Path("{id}")
  public Customer getCustomer(@PathParam("{id}") String id) {
    return null;
  }

  public static void main(String[] args) {
    buildURI("string#with#hashes"); // => http://localhost/customers/string%23with%23hashes
    buildURI("string/with/slashes"); // => http://localhost/customers/string/with/slashes
  }

  public static void buildURI(String id) {
    UriBuilder builder = UriBuilder.fromUri("http://localhost");
    builder.path(JerseyTest.class).path(JerseyTest.class, "getCustomer");
    URI uri = builder.build(id);
    System.out.println(uri);
  }
}

# 的编码按照我的预期进行,但 / 的编码却没有。我尝试使用 builder.build(URLEncoder.encode(id)) 代替,但随后 UriBuilder 对 % 进行编码,因此您会得到 .../string %252Fwith%252Fslashes

对我来说,它编码 #% 但不是 / 似乎不一致,但我怀疑它有一个很好的理由,但我不是看到。所以我的问题是:

  1. 如何让 UriBuilder 给我 .../string%2Fwith%2Fslashes,这是导致 Jersey 使用 调用 getCustomer 的 URI >id 等于字符串/带/斜杠编辑:我发现了一种解决此问题的方法:builder.buildFromEncoded(URLEncoder.encode(id))。不过,让这个问题悬而未决,希望得到第二部分的答案......
  2. 更一般地说,为什么 UriBuilder.build 编码一些特殊字符,而不是其他字符?

我发现 如何对 URI 参数值进行编码?,其中接受的答案是“使用 UriBuilder”。嗯,我正在使用它,但显然我使用方式不正确。

I have a REST API which is fairly typical, except that the id's of resources are not integers, but strings, which often contain / characters. So if a customer's id is string/with/slashes then the URI for that customer should be http://localhost/customers/string%2Fwith%2Fslashes. When returning a list of customers, I want to construct that URI with a UriBuilder so I can put it in the href of ATOM-style link elements. But it doesn't quite work; here's a small test class that shows what I mean:

@Path("/customers")
public class JerseyTest {

  @Path("{id}")
  public Customer getCustomer(@PathParam("{id}") String id) {
    return null;
  }

  public static void main(String[] args) {
    buildURI("string#with#hashes"); // => http://localhost/customers/string%23with%23hashes
    buildURI("string/with/slashes"); // => http://localhost/customers/string/with/slashes
  }

  public static void buildURI(String id) {
    UriBuilder builder = UriBuilder.fromUri("http://localhost");
    builder.path(JerseyTest.class).path(JerseyTest.class, "getCustomer");
    URI uri = builder.build(id);
    System.out.println(uri);
  }
}

The #'s get encoded as I would expect but the /'s don't. I tried using builder.build(URLEncoder.encode(id)) instead, but then the UriBuilder encodes the %'s so you get .../string%252Fwith%252Fslashes!

It seems inconsistent to me that it encodes # and % but not /, but I suspect there is a good reason for it which I am not seeing. So my question is:

  1. How can I get UriBuilder to give me .../string%2Fwith%2Fslashes, which is the URI that causes Jersey to call getCustomer with id equal to string/with/slashes? edit: I discovered a way to solve this: builder.buildFromEncoded(URLEncoder.encode(id)). Leaving this question open though, in hopes of getting an answer to the second part...
  2. More generally, why does UriBuilder.build encode some special characters, but not others?

I found How do I encode URI parameter values?, where the accepted answer says "Use UriBuilder." Well, I am using it, but apparently I'm using it incorrectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-11-21 20:56:53

这似乎是一个已确认的问题:

http://java.net/jira/browse/JAX_RS_SPEC-70

你的解决方法听起来不错。

This seem to be a confirmed issue:

http://java.net/jira/browse/JAX_RS_SPEC-70

Your workaround sounds good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文