When to use @QueryParam vs @PathParam

I'd recommend putting any required parameters in the path, and any optional parameters should certainly be query string parameters.

For example, to get all the posts on my blog I request

GET: myserver.com/myblog/posts


to get the post with id = 123, I would request

GET: myserver.com/myblog/posts/123


but to filter my list of posts, and get all posts since Jan 1, 2013, I would request

GET: myserver.com/myblog/posts?since=2013-01-01


In the first example "posts" identifies a specific entity (the entire collection of blog posts). 
In the second example, "123" also represents a specific entity (a single blog post). 
But in the last example, the parameter "since=2013-01-01" is a request to filter the posts collection not a specific entity. 

Pagination and ordering would be another good example, i.e.

GET: myserver.com/myblog/posts?page=2&order=backward


Hope that helps. :-)

Comments