0

I'm getting my teeth into designing a RESTful web-app in JAVA with Jersey.

I have a design question.

Lets say I want to return all the messages for a user. Now I have exposed the messages at the /messages uri.

Now when I do a GET call to the server, so use @QueryParam to do this?

So the url to return all the messages for user 1234 would be localhost/messages?user_id=1234

Or is there a better way to do this? Note, I have not got any user authentication built yet, nor do I know how to implement that piece yet.

cнŝdk
  • 28,676
  • 7
  • 47
  • 67
Tom O'Brien
  • 1,637
  • 4
  • 30
  • 66
  • Typically, the URI path should identify a resource, and the query param is used to sort it. (So query params can be used to filter / sort the resource response) – Oliver McPhee Jul 07 '15 at 13:52
  • Actually there is [no standardized approach](http://stackoverflow.com/questions/4024271/rest-api-best-practices-where-to-put-parameters) on where to put certain data. The original intent for path parameters was to group resources hierarchically while query parameters group non-hierarchically. Furthermore, you can limit certain resources through Matrix-Parameters - though, they are not used that often. REST does not dictate how you use them, but leves the dicision to you! – Roman Vottner Jul 07 '15 at 13:59

1 Answers1

1

The best option in this case is to use path parameter. your url should look something like

localhost/messages/user/1234

The @Path annotation's value is a relative URI path.

URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. Variables are denoted by curly braces.

example :

   @Path("/user/{userID}")
cнŝdk
  • 28,676
  • 7
  • 47
  • 67
KDP
  • 1,353
  • 6
  • 13
  • Cheers - that makes a lot of sense! – Tom O'Brien Jul 07 '15 at 13:57
  • 1
    @TomO'Brien I'm not a big fan of this. I usually go with, which resource can exist without the other and vice-versa. A message usually can't exist without a user, a user though can exist without a message - therefore I'd use `localhost/users/1234/messages` instead - but this is rather a choice you have to make – Roman Vottner Jul 07 '15 at 14:03