【问题标题】:Can Spring MVC's REST framework accept query strings rather than PathVariables?Spring MVC REST 框架可以接受查询字符串而不是路径变量吗?
【发布时间】:2012-09-03 19:30:22
【问题描述】:

在我阅读的所有关于 Spring 3 对 Spring MVC 的 RESTful 添加的教程和文章中,我只见过一种用于传递查询数据的方法,即通过 @PathVariablelike so

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

它会响应 http://www.example.com/myservlet/shops/{name} 之类的东西,它可以评估为 http://www.example.com/myservlet/shops/thebestshoparound

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

这似乎是一个非常简单的问题,但我在网上的任何地方都找不到。

【问题讨论】:

    标签: spring-mvc query-string spring-3 path-variables


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2011-12-16
      • 1970-01-01
      • 2020-12-28
      • 2011-11-20
      相关资源
      最近更新 更多