28

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.

1 Answer 1

48

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.