在 Java 中处理 URI 中的多个参数(REST 方式)
我一直在 Java/Jersey 中开发一个小型 Web 服务,它从 XML 文件中包含的客户端读取用户信息列表。目前,除了一个方面之外,我在所有方面都具有此功能:在 URI 中使用多个参数来表示拉取多组用户信息或多组客户端信息。我有一个当前有效的版本,但不是最好的方法,也不是项目描述所要求的。
目前,我的代码如下所示:
@Path("Client/{client}/users")
public class UserPage
{
@GET
@Produces(MediaType.TEXT_HTML)
public String userChoice(@PathParam(value = "client") final String client)
{****Method here which handles a list of 'users'****}
@GET
@Path("{name}")
@Produces(MediaType.TEXT_HTML)
public String userPage(@PathParam(value = "client") final String client, @PathParam(value = "name") final String name)
{****Method here which handles 'user' information****}
第一个方法处理来自 URI 中由“{client}”表示的“客户端”的用户列表。第二种方法传递 URI 中由“{name}”表示的“用户”信息。两者都可以通过一个参数来运行。目前,为了处理多个“用户”,我使用“{name}”逗号分隔,如“Client/Chick-Fil-A/users/Phil,Bradley”。我可以在使用 @PathParam 后解析它并创建这些“用户”的数组,但同样,我觉得这不是处理这个问题的最佳方法,并且项目描述需要不同的东西。
有没有办法使用格式为“Client/Chick-Fil-A;cd=Phil,Bradley”的 URI 来完成相同的任务? ( ;cd= 是给我带来最大麻烦的。) 我还需要能够对多个客户端使用此格式,即“Client;cd=Chick-Fil-A,Subway/users;cd=Phil,Bradley”。
编辑:澄清该项目: 客户信息包含在 6 个单独的文件中。每个文件都有相同的 3 个用户(这实际上是一个概念证明)。我需要能够提取不同的信息子集,例如,来自麦当劳和 Chick-Fil-A 的用户 Phil,或者来自麦当劳的用户 Phil 和 Peter,或者来自所有客户的名为 Peter 的用户,等等。
I've been working on a small scale web service in Java/Jersey which reads lists of user information from clients contained in XML files. I currently have this functioning in all but one aspect: using multiple parameters in the URI to denote pulling multiple sets of user information or multiple sets of client information. I have a version which currently works, but is not the best way nor what the project description calls for.
Currently, my code looks like this:
@Path("Client/{client}/users")
public class UserPage
{
@GET
@Produces(MediaType.TEXT_HTML)
public String userChoice(@PathParam(value = "client") final String client)
{****Method here which handles a list of 'users'****}
@GET
@Path("{name}")
@Produces(MediaType.TEXT_HTML)
public String userPage(@PathParam(value = "client") final String client, @PathParam(value = "name") final String name)
{****Method here which handles 'user' information****}
The first method handles a list of users from a 'client' denoted by "{client}" in the URI. The second method delivers 'user' information denoted by "{name}" in the URI. Both will function with a single argument. Currently, in order to handle multiple 'users' I have "{name}" comma separated like "Client/Chick-Fil-A/users/Phil,Bradley". I can parse this after using @PathParam and create an array of these 'users', but again, I feel this is not the best way to handle this, and the project description calls for something different.
Is there a way to accomplish this same task with a URI formatted as "Client/Chick-Fil-A;cd=Phil,Bradley"? (The ;cd= is what's giving me the most trouble.)
I also need to be able to use this format for multiple clients, i.e. "Client;cd=Chick-Fil-A,Subway/users;cd=Phil,Bradley".
Edit: To clarify the project:
The client information is contained in 6 separate files. Each of these files has the same 3 users (this is a proof of concept, effectively). I need to be able to pull different subsets of information, for instance, user Phil from McDonalds and Chick-Fil-A, or users Phil and Peter from McDonalds, or users named Peter from all clients, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在 URL 路径中使用“=”,因为它是保留字符。但是,还有许多其他字符可以用作分隔符,例如“-”和“,”。因此,您可以使用“-”代替“=”。如果您真的想使用“=”,那么您必须 URL 编码< /a> 它;但是,我强烈建议不要这样做,因为它可能会使事情变得更加复杂。
您可以在此处查看 URL 字符串的语法:
http://www.w3。 org/Addressing/URL/url-spec.txt
复制并搜索以下字符串以跳至路径语法:
也就是说,我相信 HTTP 请求通常仅用于请求单个资源。所以我个人的意见是不要按照您实施的方式实施服务。为了获得多个客户,我将使用查询参数作为过滤器,如下所示:
编辑:从您获得的业务案例来看,您可能需要这样的服务
所以说您想从所有客户那里获得 Peter可以有这种形式的请求:
同样,如果您想从星巴克得到杰克和彼得,那么您可以这样做:
希望这会有所帮助。
You cannot use '=' in the URL path since it's a reserved character. However there are many other character you can use as delimiters such as '-' and ','. So instead of '=' you can use '-'. If you really really want to use '=' then you will have to URL-encode it; however, I would strongly recommend against this because it may make things more complicated then it should be.
You can see the grammar of the URL string here:
http://www.w3.org/Addressing/URL/url-spec.txt
Copy and search the following string to skip to the path grammar:
That said, I believe HTTP request is usually used for request single resource only. So my personal opinion is to not implement the service the way you implemented. For getting multiple clients I would use query parameters as filters like this:
Edit: From the business case you got there, it seems like you probably need service like
So say you want to get Peter from all clients then can have a request of this form:
Similarly, if you want to get Jack and Peter from Starbucks then you can do:
Hopefully this helps.
查询字符串具有以下语法,并且您可以有多个同名参数:
其中 query_string 具有以下语法:
有关更多详细信息,请查看 Wikipedia 中的此条目:http://en.wikipedia.org/wiki/Query_string
Query strings have the following syntax and you can have multiple parameters with the same name:
where query_string has the following syntax:
For more details check out this entry in Wikipedia: http://en.wikipedia.org/wiki/Query_string