Search code examples
springspring-bootspring-mvccontrollerrequest-mapping

RequestParam with map is unnecessarily binding other request params in GET request


I have a GET request like this :-

    @GetMapping(value = "/foo")
          public Model getSomething(
          @RequestParam(value = "a", required = true) String a,
          @RequestParam(value = "b") String b,
          @RequestParam(value = "c") int c,
          @RequestParam Map<String, String> map) { 

}

The problem is when I make get request a,b,c gets included in map. I have used mapping like this:-

/foo?a=A&b=B&c=100&key1=value1&key2=value2

Currently what is happening is map is having all the request param values. For eg

a-"a"

b-"b"

c-100

key1-value1

key2-value2

The requirement is to have map with only key1 and key2 as keys. I want to use map to extract key1 and key2 only. How can this be achieved.


Solution

  • As mentioned in the comments, you can't exclude params for the @RequestParam map.

    Do your client have the option to send those params with the body as a json object instead of as request parameters? A request body isn't normally sent in GET Requests, but you could do it. Then you would change @RequestParam<String, String> map) to @RequestBody Map<String, String> map)