java 如何在 GET 请求中的休息控制器中获取查询参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26473381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:59:12  来源:igfitidea点击:

How to get query parameters in a rest controller in a GET request?

javacontrollerspring-bootrestful-url

提问by Nik

This should probably be a one-liner, but I am not used to Spring or SpringBoot, and so I'm having troubles.

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

I am building a RESTful service, with query parameters. For example: http://myweatherapi.com:8080/foo?zip=14325&prop=humidity.

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

I am trying a SpringBoot's template within which I have this controller:

我正在尝试一个 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!!";
    }

}

My question is: how can I access the full URL/query parameters?

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

回答by Gurkan ?lleez

Here's an example:

下面是一个例子:

@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!!";
}