queryparm support in spring for restful web services

Ram Sharan Mittal picture Ram Sharan Mittal · Sep 21, 2013 · Viewed 24.9k times · Source

How can I get queryparam support of restful web service by spring in true restful manner...

for example i have url like following

localhost:8080/myapp/booksearch/title/{title}/author/{author}?sortby=relevance&year=2013

title and author I can get by @PathVariable .....

where i want sortby and year optional..

Answer

Sotirios Delimanolis picture Sotirios Delimanolis · Sep 21, 2013

You can use the @RequestParam annotation on method parameters

@RequestMapping (...)
public String getBooks(@RequestParam(required = false, value = "sortby") String sortBy, @RequestParam(required = false, value = "year") String year) {...}

The @RequestParam annotation also has a defaultValue attribute to use as a value if the request parameter isn't provided. Without it, if the parameter is not provided, null will be passed as the argument for that parameter.