用Java处理URI中的多个参数(RESTful) [英] Handling multiple parameters in a URI (RESTfully) in Java

查看:123
本文介绍了用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.

当前,我的代码如下:

@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.

是否有一种方法可以通过URI格式为"Client/Chick-Fil-A; cd = Phil,Bradley"来完成相同的任务? (; cd =是给我最大的麻烦.) 我还需要能够将这种格式用于多个客户端,即客户端; cd = Chick-Fil-A,地铁/用户; cd = Phil,布拉德利".

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个用户(实际上,这是概念证明).我需要能够提取不同的信息子集,例如,来自麦当劳和Chick-Fil-A的用户Phil,或来自麦当劳的用户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编码;但是,我强烈建议您不要这样做,因为它可能会使事情变得比应有的复杂.

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- spec.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

类似地,如果您想从星巴克(Starbucks)获得杰克(Jack)和彼得(Peter),则可以这样做:

Similarly, if you want to get Jack and Peter from Starbucks then you can do:

/users?name=Peter,Jack&client=Starbucks

希望这会有所帮助.

这篇关于用Java处理URI中的多个参数(RESTful)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆