1

I have some rest API

@GetMapping(value = "/getSomething")
public String getSomething(@RequestParam(value = "name") String name)

Caller to the API can provide multiple instances of the name parameter

/getSomething?name=something&name=somethingElse

In the API code the name variable will be received as "something,somethingElse"

Is there a way yo prevent this behavior ?

accept only one "name" parameter?

or return "400" "bad request" for such a case?

Thanks

Maayan Hope
  • 1,482
  • 17
  • 32
  • I am just guessing here, but I think it is Http protocol issue rather then spring boot. So, I would try to see if you have ',' in your input - fail it. but then what if ',' is actually part of the input... – Michael Gantman Mar 20 '22 at 13:54
  • well as you said, the input may contain "," – Maayan Hope Mar 20 '22 at 15:00
  • 3
    Are you looking for: https://stackoverflow.com/questions/22817430/handle-duplicate-values-for-spring-requestparam ? – VoiceOfUnreason Mar 21 '22 at 03:44
  • For permanent solution, refere as @VoiceOfUnreason mentioned, but to stop the bleeding you can do something like this -> if(Arrays.asList(name.split(",")).size()>1) { throw exception } – Harsh Apr 08 '22 at 10:29

0 Answers0