【问题标题】:How to get query parameters in a rest controller in a GET request?如何在 GET 请求中获取休息控制器中的查询参数?
【发布时间】:2014-10-20 19:25:42
【问题描述】:

这应该是单行的,但是我不习惯Spring或SpringBoot,所以我遇到了麻烦。

我正在构建一个带有查询参数的 RESTful 服务。例如:http://myweatherapi.com:8080/foo?zip=14325&prop=humidity

我正在尝试一个 SpringBoot 的模板,其中有这个控制器:

@RestController
public class ServiceController {

    private static Logger LOG = LoggerFactory.getLogger(ServiceController.class);

    @RequestMapping("/foo")
    public String foo(@QueryParam("foo") String foo) {
        requestContextDataService.addNamedParam("foo", foo);

        // how can I access the full URL/query params here?

        return "Service is alive!!";
    }

}

我的问题是:如何访问完整的 URL/查询参数

【问题讨论】:

    标签: java controller spring-boot restful-url


    【解决方案1】:

    这是一个例子:

    @RequestMapping("/foo")
    public String foo(HttpServletRequest request,@QueryParam("foo") String foo) {
        requestContextDataService.addNamedParam("foo", foo);
    
        // how can I access the full URL/query params here?
        request.getRequestURL() 
        request.getQueryString() 
    
        return "Service is alive!!";
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 2010-10-24
      • 2012-11-06
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多