在 Java 中处理 URI 中的多个参数(REST 方式)

发布于 2024-11-09 16:25:03 字数 1255 浏览 1 评论 0原文

我一直在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-11-16 16:25:03

您不能在 URL 路径中使用“=”,因为它是保留字符。但是,还有许多其他字符可以用作分隔符,例如“-”和“,”。因此,您可以使用“-”代替“=”。如果您真的想使用“=”,那么您必须 URL 编码< /a> 它;但是,我强烈建议不要这样做,因为它可能会使事情变得更加复杂。

您可以在此处查看 URL 字符串的语法:

http://www.w3。 org/Addressing/URL/url-spec.txt

复制并搜索以下字符串以跳至路径语法:

 path                    void |  segment  [  / path ] 

 segment                 xpalphas

也就是说,我相信 HTTP 请求通常仅用于请求单个资源。所以我个人的意见是不要按照您实施的方式实施服务。为了获得多个客户,我将使用查询参数作为过滤器,如下所示:

Client/{cName}/users?filters=<value1>,<value2> ...

编辑:从您获得的业务案例来看,您可能需要这样的服务

/users?<filters>
/clients?<filters>

所以说您想从所有客户那里获得 Peter可以有这种形式的请求:

/users?name=Peter

同样,如果您想从星巴克得到杰克和彼得,那么您可以这样做:

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

希望这会有所帮助。

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:

 path                    void |  segment  [  / path ] 

 segment                 xpalphas

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

Edit: 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

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

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

Hopefully this helps.

朱染 2024-11-16 16:25:03

查询字符串具有以下语法,并且您可以有多个同名参数:

http://server/path/program?<query_string>

其中 query_string 具有以下语法:

field1=value1&field1=value2&field1=value3…

有关更多详细信息,请查看 Wikipedia 中的此条目:http://en.wikipedia.org/wiki/Query_string

Query strings have the following syntax and you can have multiple parameters with the same name:

http://server/path/program?<query_string>

where query_string has the following syntax:

field1=value1&field1=value2&field1=value3…

For more details check out this entry in Wikipedia: http://en.wikipedia.org/wiki/Query_string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文