M Shahid
M Shahid

Reputation: 11

How to extract Query Parameters from request in Spring RestController method?

URL: /api/v1/user ? type=abc & company=xyz

In an API call, we can extract the query param by using @QueryParam annotation. But, we have to define the query parameter key for mapping.

@QueryParam("type") String type
@QueryParam("company") String company

In case, we have dynamic parameters coming in like page=2 & limit=10 & skip=20 & type=abc ... and we are making another call to a different service using a rest client. Here, we have to pass all the query parameters whatever are received in the request to that service.

How can we read and add these query params to the Request?

Upvotes: 1

Views: 3422

Answers (1)

Martin Lund
Martin Lund

Reputation: 1169

You could try to use a Map and define the queryparams in that like so.

@RequestParam Map<String,String> params

Then you can find the required variables in the params map as such:

params.get("type");

Then when you make your request, you can create some logic for if the map contains the certain param, then use it :)


When you define @RequestParam without a specific parameter to target, it will extract all params and inject into the map.

Upvotes: 2

Related Questions