Spring MVC的REST框架可以接受查询字符串而不是PathVariables吗?

Ken*_*ows 28 path-variables spring-mvc query-string spring-3

在我阅读的关于Spring 3的Spring MVC的RESTful添加的所有教程和文章中,我只看到过一种方法,用于通过a传递查询数据@PathVariable,如下所示:

@RequestMapping(value="/shops/{name}", method=RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

这将回应类似的东西http://www.example.com/myservlet/shops/{name},可以评估http://www.example.com/myservlet/shops/thebestshoparound.

我的问题是:是否可以设置一个RESTful接口,它接受基于经典查询字符串的请求,例如http://www.example.com/myservlet/shops?name=thebestshoparound,而不是PathVariables

这似乎是一个非常简单的问题,但我无法在网上找到它.

nic*_*dos 48

是的,使用注释@RequestParam,这是一个例子:

public @ResponseBody Shop getShopInJSON(@PathVariable String name, @RequestParam(value="query", required=false) String query) {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)