Jersey 是否支持 JAX-RS 路径注释中的美元符号?
我希望能够访问以下其余 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
找到了答案。将美元符号放在字符类中就可以了。
上面的内容与下面的 URL 匹配。
Found the answer. Placing the dollar sign in a character class did the trick.
The above matches the following URLs.
美元符号是 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.
您在 @Path 中使用了错误的斜杠,
这也不是使用 PathParam 的正确方法。如果您尝试检索 /helloworld 之后的值,您应该执行以下
编辑
无法让它与 $ 一起工作
You are using the wrong slash in the @Path
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
Edit
Couldn't get it to work with a $
问题早已解决,但也许这会帮助将来寻找类似问题的人。
我们在处理这个问题时发现的方法是编写一个类,将编码符号替换回美元符号本身。我们在 RestEasyClient 中注册了该类。
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.