Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey支持JAX-RS的美元登录路径注释吗?_Java_Regex_Jersey_Jax Rs - Fatal编程技术网

Java Jersey支持JAX-RS的美元登录路径注释吗?

Java Jersey支持JAX-RS的美元登录路径注释吗?,java,regex,jersey,jax-rs,Java,Regex,Jersey,Jax Rs,我希望能够访问以下rest URL: 美元计数 第一个URL工作正常。我在使用JAX-RS的Jersey实现时遇到了$countURL问题 这是资源的代码 @Path("/helloworld") public class HelloWorldResource { @GET @Produces("text/plain") public String getClichedMessage() { return "Hello World!"; }

我希望能够访问以下rest URL:

  • 美元计数
第一个URL工作正常。我在使用JAX-RS的Jersey实现时遇到了$countURL问题

这是资源的代码

@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 producer应用程序。

美元符号是URL中的特殊字符,需要进行编码,我担心:


如果你感兴趣的话,你要找的字符是%24,但是如果你在java中,阅读这个类可能是值得的。我没有玩过Jersey,但Java完全有能力为您完成这项艰巨的工作。

您在@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

找到了答案。在字符类中放置美元符号就达到了目的

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

    return "Hello count";
}
以上内容与以下URL匹配

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

这个问题早就解决了,但这可能会帮助将来寻找类似问题的人

我们在处理这个问题时发现的方法是编写一个类,将编码符号替换回美元符号本身。我们在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);
}

我很确定$sign在URL编码规范中具有语义含义。我无法想象在常规URL中使用$sign而不进行编码。在URL中使用$sign而不进行编码是合法的。它大量用于开放数据协议,这是一种基于rest的协议。请参阅美元符号编码不是强制性的。美元符号在URI RFC、RFC 3986中被标识为保留字符,因为它对某些特定的URI方案具有特殊意义(尤其是
gopher:
)。它在
http:
URI方案中没有特殊意义。Rohaq,我相信MetaEd是正确的。有关大量使用美元登录URL的基于rest的协议,请参阅。啊,好吧,那我错了!不过,为了避免进一步的问题,可能仍然值得对URI进行正确编码。嗨,Michael。尝试了您建议的第一个代码示例,但无效。第二个示例不适用,因为我需要匹配文本“$count”字符串。@Jerome你说得对,修正斜杠后仍然不起作用。