Jersey 和 Odata 密钥路径参数格式
我现在有一个使用 Jersey 的 RESTful api,并将其转换为符合 OData 标准。有一些事情我还没有转换,但会实现的,目前并不重要。我需要转换的重要内容之一是关键路径参数。 Odata 有将 key 括在括号中的标准。因此,在此示例中,myapi.com/product(1) - 是获取 id 为 1 的产品的 OData 调用。目前,在我的系统中,使用此 myapi.com/product/1 是可能的。
当我将括号添加到路径时参数我收到 404 错误。我的类级别路径是@Path("/product"),我的方法级别路径是@Path("({id})"),并且过去是@Path("/{id}")。我尝试添加括号作为变量计划的一部分,以在方法中将它们去掉,并且我尝试使用一些正则表达式 @Path("{id : regex stuff}") 格式化 id,但都不起作用。
如果我使我的方法路径参数像这样 @Path"/({id})") - 那么调用是 myapi.com/product/(1),它工作正常。括号显然不是问题。 Jersey 似乎使用正斜杠将 uri 分割成路由块,并且感觉 id 和根资源名称之间没有正斜杠,然后什么也没找到。这是有道理的。
有没有办法改变 Jerseys 与某些正则表达式或其他内容匹配 uri 字符串的方法?有人用过 Odata 的 Jersey 吗?我宁愿不使用 odata4j 只是为了解决这个问题,似乎应该有一种方法可以让它发挥作用。
我做了什么: 根据 Pavel Bucek 的回答,我确实独立于我用于安全性的过滤器实现了 ContainrRequestFilter 。就我而言,我没有查看是否存在,我只是尝试进行替换。
try
{
String uriString = request.getRequestUri().toString();
uriString = uriString.replaceAll("(\(|\)\/?)", "/");
request.setUris(request.getBaseUri(), new URI(uriString));
} catch (final Exception e)
{
}
return request;
I have a RESTful api using Jersey right now, and am converting it to be OData standard compliant. There are a few things I have not converted yet, but will get there, and is not important at this moment. One of the things I need to convert that is important is the key path params. Odata has the standard of making the key wrapped in parenthesis. So in this example myapi.com/product(1) - is the OData call to get a product whose id is 1. Currently that is possible in my system with this myapi.com/product/1
When I add the parenthesis to the path parameter I get a 404 error. My class level path is @Path("/product") and my method level path is @Path("({id})"), and use to be @Path("/{id}"). I've tried adding the parenthesis as part of the variable planning to strip them off in the method, and I've tried formatting the id with some regex @Path("{id : regex stuff}"), and neither works.
If I make my method path parameter like this @Path"/({id})") - so the call is myapi.com/product/(1), it works fine. The parenthesis is not the issue obviously. It seems the Jersey splits the uri into chunks using the forward slashes for the routing, and sense there is no forward slash between the id an root resource name, then nothing is found. It makes sense.
Is there a way to change Jerseys method of matching uri strings with some regex or something? Has anyone used Jersey with Odata? I would rather not use odata4j just for the resolution to this issue, it seems like there should be a way to get this to work.
What I did:
Based on Pavel Bucek's answer I did implement a ContainrRequestFilter independently to the filter I use for security. In my case I didn't look to see if existed, I just tried to do the replace.
try
{
String uriString = request.getRequestUri().toString();
uriString = uriString.replaceAll("(\(|\)\/?)", "/");
request.setUris(request.getBaseUri(), new URI(uriString));
} catch (final Exception e)
{
}
return request;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为处理这个“协议”的最简单方法是引入 ContainerRequestFilter,它将传入 URI 中的“()$”替换为“/$”。因此,您将能够在一个应用程序中提供 OData 和标准 REST 请求。
请参阅 http://jersey。 java.net/nonav/apidocs/1.11/jersey/com/sun/jersey/spi/container/ContainerRequestFilter.html
我用来测试这个的简单过滤器案例:
两者
卷曲“http://localhost:9998/helloworld(1)”
卷曲“http://localhost:9998/helloworld/1”
现在点击相同的资源方法。 (显然,您需要改进当前的过滤器才能处理各种值,但它应该适合您)。
I think that the easiest way how to handle this "protocol" would be introducing ContainerRequestFilter, which would replace "()$" with "/$" in the incoming URI. So you will be able to serve OData and standard REST request in one app.
See http://jersey.java.net/nonav/apidocs/1.11/jersey/com/sun/jersey/spi/container/ContainerRequestFilter.html
Simple filter I used to test this case:
both
curl "http://localhost:9998/helloworld(1)"
curl "http://localhost:9998/helloworld/1"
hit same Resource method now. (Obviously you'll need to improve current filter to be able to handle various values, but it should work for you).