문제

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top