【问题标题】:Does Jersey support dollar sign in Path annotation of JAX-RS?Jersey 是否支持 JAX-RS 的路径注释中的美元符号?
【发布时间】:2012-01-09 17:32:06
【问题描述】:

我希望能够访问以下其余 URL:

第一个 URL 工作正常。我在使用 JAX-RS 的 Jersey 实现的 $count URL 时遇到问题。

这是资源的代码。

@Path("/helloworld")
public class HelloWorldResource {
    @GET
    @Produces("text/plain")
    public String getClichedMessage() {
        return "Hello World!";
    }

    @GET
    @Path("\\$count")
    @Produces("text/plain")
    public String getClichedMessage(
            @PathParam("\\$count") String count) {

        return "Hello count";
    }
}

我也在 @Path 和 @PathParam 中尝试过 "$count",但也没有用。

注意:如果我从上面的所有代码中删除美元符号,那么它适用于 URL localhost:9998/helloworld/count。但是我需要美元符号出现在 URL 中,因为这将是一个 OData 生产者应用程序。

【问题讨论】:

  • 我很确定 $ 符号在 URL 编码规范中具有语义含义。我无法想象你可以在不编码的情况下在常规 URL 中使用它。
  • 美元符号在 URL 中使用是合法的,无需编码。它在开放数据协议中大量使用,这是一个基于休息的协议。见link

标签: java regex jersey jax-rs


【解决方案1】:

找到答案。将美元符号放在字符类中就可以了。

@GET
@Path("{count : [$]count(/)?}")
@Produces("text/plain")
public String getClichedMessageCount(
        @PathParam("count") String count) {

    return "Hello count";
}

以上匹配以下网址。

  • localhost:9998/helloworld/$count
  • localhost:9998/helloworld/$count/
  • localhost:9998/helloworld/$count?$filter=blah
  • localhost:9998/helloworld/$count/?$filter=blah

【讨论】:

    【解决方案2】:

    美元符号是 URL 中的特殊字符,恐怕需要这样编码:

    http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

    如果您有兴趣,您正在寻找的字符是 %24,但如果您使用的是 java,那么阅读 java.net.URI 类可能是值得的。我没有玩过 Jersey,但 Java 完全有能力在这里为您完成繁重的工作。

    【讨论】:

    • 对美元符号进行编码不是强制性的。美元符号在 URI RFC RFC 3986¹ 中被标识为保留字符,因为它对某些特定的 URI 方案(特别是 gopher:)具有特殊含义。在http: URI 方案中没有特殊含义。
    • Rohaq,我相信 MetaEd 是正确的。请参阅Open Data Protocol 了解基于 REST 的协议,该协议大量使用 URL 中的美元符号。
    • 啊,好吧,那我错了!尽管为了避免进一步的问题,它可能仍然值得正确编码 URI。
    【解决方案3】:

    您在@Path 中使用了错误的斜线

    @GET
    @Path("/$count")
    @Produces("text/plain")
    public String getClichedMessage(
            @PathParam("\\$count") String count) {
    
        return "Hello count";
    }
    

    这也不是使用 PathParam 的正确方法。如果您尝试在 /helloworld 之后检索值,您应该执行以下操作

    @GET
    @Path("/{$count}")
    @Produces("text/plain")
    public String getClichedMessage(
            @PathParam("$count") String count) {
    
        return "Hello count";
    }
    

    编辑 无法使用 $

    @Path("count") // works
    @Path("/count") // works
    @Path("\\count") // does not work
    @Path("$count") // does not work
    @Path("/$count") // does not work
    

    【讨论】:

    • 嗨迈克尔。尝试了您建议的第一个代码示例,但没有成功。第二个示例不适用,因为我需要匹配文字“$count”字符串。
    • @Jerome 你说得对,修复斜线后还是不行。
    【解决方案4】:

    问题早已解决,但也许这将有助于将来寻找类似问题的人。

    我们在处理这个问题时发现的方法是编写一个类,将编码符号替换回美元符号本身。我们在 RestEasyClient 中注册了该类。

    public class LoggingFilter implements ClientRequestFilter{
    
    @Override
    public void filter(ClientRequestContext context) throws IOException {
       //replace uri in context...
       String uri = context.getUri();
       //regex to replace $ sign in uri
       //set new uri in context so request goes to correct url
       context.setUri(uri);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 2015-07-22
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      相关资源
      最近更新 更多