Nik
Nik

Reputation: 5755

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

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

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

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

@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?

Upvotes: 2

Views: 16163

Answers (1)

Gurkan İlleez
Gurkan İlleez

Reputation: 1583

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

Upvotes: 6

Related Questions