Jersey 是否支持 JAX-RS 路径注释中的美元符号?

发布于 2024-12-25 21:30:54 字数 893 浏览 0 评论 0原文

我希望能够访问以下其余 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 生产者应用程序。

I would like to be able to access the following rest URLs:

The first URL works fine. I am having trouble with the $count URL using Jersey implementation of JAX-RS.

Here is the code for the resource.

@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";
    }
}

I've also tried "$count" in both @Path and @PathParam but that didn't work either.

Note: If I remove the dollar sign from all the code above then it works fine for the URL localhost:9998/helloworld/count. However I need the dollar sign to be there in the URL because this will be an OData producer application.

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

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

发布评论

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

评论(4

往昔成烟 2025-01-01 21:30:54

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

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

    return "Hello count";
}

上面的内容与下面的 URL 匹配。

  • 本地主机:9998/helloworld/$count
  • 本地主机:9998/helloworld/$count/
  • 本地主机:9998/helloworld/$count?$filter=blah
  • 本地主机:9998/helloworld/$count/?$filter=blah

Found the answer. Placing the dollar sign in a character class did the trick.

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

    return "Hello count";
}

The above matches the following URLs.

  • localhost:9998/helloworld/$count
  • localhost:9998/helloworld/$count/
  • localhost:9998/helloworld/$count?$filter=blah
  • localhost:9998/helloworld/$count/?$filter=blah
洛阳烟雨空心柳 2025-01-01 21:30:54

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

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

您要查找的字符是 %24,如果您有兴趣,但如果您使用的是 java,读起来在 java. net.URI 类可能值得。我没有使用过 Jersey,但 Java 完全有能力在这里为您完成艰苦的工作。

Dollar signs are special characters in URLs, and need to be encoded as such, I'm afraid:

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

The character you're looking for is %24, if you're interested, though if you're in java, reading up on the java.net.URI class might be worth it. I've not played with Jersey, but Java is more than capable of doing the hard work for you here.

剑心龙吟 2025-01-01 21:30:54

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

You are using the wrong slash in the @Path

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

    return "Hello count";
}

Also that is not the correct way to use PathParam. If you are trying to retrieve the value after /helloworld you should do the following

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

    return "Hello count";
}

Edit
Couldn't get it to work with a $

@Path("count") // works
@Path("/count") // works
@Path("\\count") // does not work
@Path("$count") // does not work
@Path("/$count") // does not work
撩动你心 2025-01-01 21:30:54

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

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

Question is long solved but maybe this will help someone in the future looking for a similar problem.

The way we found when dealing with this problem is to write a class which replaces the encoded sign back to the dollar sign itself. We registered the class in our 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文