@RequestParam name includes square brackets []

2.9k views Asked by At

I am writing a feign client to consume an endpoint of a PHP API.

I need to call an endpoint which is like :

www.myapp.com/number.php?number[]=1

My Feign Client looks like this:

@FeignClient(name = "testProxy", url = "${service.url}")
public interface NumberProxy {

    @RequestMapping(value = INumber.URL, method = RequestMethod.GET)
    public String getEvents(@RequestParam("numbers[]") Integer number);
}

The problems is number[].

If I see the feign log to check the GET URL, this is what I see.

GET [https://www.myapp.com/number.php?number[]={number[]}][1] HTTP/1.1

The number[] is not replaced by the actual value and that is what the API call is failing.

Is there a way to deal with this?

P.S. I know that the PHP API should not have a query parameter like this, but it is what it is and I can not change that.

And I have also tried with List<Integer> for the number variable, but output is same.

2

There are 2 answers

1
Yuriy Tsarkov On

Are we talking about a org.springframework.web.bind.annotation.RequestParam if so it shouldn't be the problem of square brackets. For me it works fine:

@RequestMapping(value = "/api/somepath", method = RequestMethod.POST)
  public void uploadData(
      @RequestPart("file") MultipartFile fileUpload, HttpServletRequest request,
      HttpServletResponse response, @RequestParam(name = "param[]") Integer number) {
  LOGGER.trace("paramValue=[{}]", number)
}

logs a value passed through a client

What if the problem is in parameter naming? In the first occurence you write numberS[] but further it named as number[]

2
Maciej Kowalski On

You should name your parameter just as numbers without the brackets and change the type to a List:

@RequestMapping(value = INumber.URL, method = RequestMethod.GET)
public String getEvents(@RequestParam("numbers") List<Integer> number);