Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Spring MVC's REST framework accept query strings rather than PathVariables?

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.

like image 945
Ken Bellows Avatar asked Sep 10 '12 21:09

Ken Bellows


Video Answer


1 Answers

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 } 
like image 120
nickdos Avatar answered Oct 14 '22 05:10

nickdos