Handling multiple parameters in a URI (RESTfully) in Java

后端 未结 2 1061
日久生厌
日久生厌 2020-12-19 16:58

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 i

相关标签:
2条回答
  • 2020-12-19 17:28

    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.

    0 讨论(0)
  • 2020-12-19 17:30

    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

    0 讨论(0)
提交回复
热议问题