且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Java 中处理 URI(RESTful)中的多个参数

更新时间:2023-02-15 13:21:55

你不能在 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

希望这会有所帮助.