In all the tutorials and articles I have read regarding Spring 3's RESTful additions to Spring MVC, I have only ever seen one way for the use to pass in query data, via a @PathVariable
, like so:
@RequestMapping(value="/shops/{name}", method=RequestMethod.GET) public @ResponseBody Shop getShopInJSON(@PathVariable String name) { ... }
which would respond to something like http://www.example.com/myservlet/shops/{name}
, which could evaluate to http://www.example.com/myservlet/shops/thebestshoparound
.
My question is this: Is it possible to set up a RESTful interface that takes requests based on classic query strings, e.g. http://www.example.com/myservlet/shops?name=thebestshoparound
, instead of PathVariables
?
This seems like a really simple question, but I can't find it anywhere online.
Yes, use the annotation @RequestParam
, here is an example:
public @ResponseBody Shop getShopInJSON(@PathVariable String name, @RequestParam(value="query", required=false) String query) { // do stuff }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With