在 Java 中处理 URI(RESTful)中的多个参数
我一直在使用 Java/Jersey 开发一个小型 Web 服务,它从 XML 文件中包含的客户端读取用户信息列表.我目前在所有方面都有这个功能:在 URI 中使用多个参数来表示拉取多组用户信息或多组客户端信息.我有一个当前可用的版本,但不是最好的方法,也不是项目描述所要求的.
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****}
第一种方法处理来自 URI 中{client}"表示的客户端"的用户列表.第二种方法在 URI 中传递由{name}"表示的用户"信息.两者都将使用单个参数起作用.目前,为了处理多个用户",我使用{name}"逗号分隔,如Client/Chick-Fil-A/users/Phil,Bradley".我可以在使用 @PathParam 并创建一个包含这些用户"的数组后对其进行解析,但我再次觉得这不是处理这个问题的最佳方式,并且项目描述需要一些不同的东西.
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.
有没有办法使用格式为Client/Chick-Fil-A;cd=Phil,Bradley"的 URI 来完成同样的任务?( ;cd= 是给我最大的麻烦.)我还需要能够为多个客户端使用这种格式,即Client;cd=Chick-Fil-A,Subway/users;cd=Phil,Bradley".
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".
澄清项目:客户信息包含在 6 个单独的文件中.这些文件中的每一个都有相同的 3 个用户(这是一个有效的概念证明).我需要能够提取不同的信息子集,例如,来自 McDonalds 和 Chick-Fil-A 的用户 Phil,或者来自 McDonalds 的用户 Phil 和 Peter,或者来自所有客户的名为 Peter 的用户,等等.
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.
推荐答案
你不能在 URL 路径中使用 '=',因为它是一个保留字符.但是,您可以使用许多其他字符作为分隔符,例如-"和,".因此,您可以使用-"来代替=".如果你真的想使用 '=' 那么你将不得不 URL-encode 它;但是,我强烈建议不要这样做,因为它可能会使事情变得更复杂.
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.
这里可以看到URL字符串的语法:
You can see the grammar of the URL string here:
http://www.w3.org/Addressing/URL/url-规范.txt
复制并搜索以下字符串以跳转到路径语法:
Copy and search the following string to skip to the path grammar:
path void | segment [ / path ]
segment xpalphas
也就是说,我相信 HTTP 请求通常只用于请求单个资源.所以我个人的意见是不要按照你实施的方式实施服务.为了获得多个客户端,我将使用查询参数作为过滤器,如下所示:
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:
Client/{cName}/users?filters=<value1>,<value2> ...
从你得到的商业案例来看,你可能需要像
From the business case you got there, it seems like you probably need service like
/users?<filters>
/clients?<filters>
假设你想从所有客户那里得到彼得然后可以有这种形式的请求:
So say you want to get Peter from all clients then can have a request of this form:
/users?name=Peter
同样,如果你想从星巴克得到 Jack 和 Peter,那么你可以这样做:
Similarly, if you want to get Jack and Peter from Starbucks then you can do:
/users?name=Peter,Jack&client=Starbucks
希望这会有所帮助.
相关文章