@RequestParam名称包含方括号[]

时间:2019-04-30 10:44:15

标签: java spring spring-boot spring-cloud-feign netflix-feign

我正在编写一个伪装的客户端以使用PHP API的端点。

我需要调用一个端点,例如:

  

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

我的Feign客户看起来像这样:

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

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

问题是number[]

如果我看到伪装日志以检查GET URL,这就是我所看到的。

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

number[]并没有替换为实际值,这就是API调用失败的原因。

有没有办法解决这个问题?

PS 我知道PHP API不应具有这样的查询参数,但这就是它的本质,我无法更改。

我还尝试使用List<Integer>作为number变量,但是输出是相同的。

2 个答案:

答案 0 :(得分:1)

我们是否在谈论org.springframework.web.bind.annotation.RequestParam,这不是方括号的问题。对我来说,它工作正常:

@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)
}

记录通过客户端传递的值

如果问题出在参数命名上怎么办?在第一次出现时,您编写 numberS [] ,但进一步将其命名为 number []

答案 1 :(得分:1)

您应将参数命名为numbers,而不用方括号,并将类型更改为List

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