![](/img/trans.png)
[英]@RestController and @GetMapping does not return JSON array in simple Hello World List
[英]GetMapping with JSON array parameter
我正在使用 FeignClient 和 GetMapping 与外部系统进行交互。
我的 GetMapping 定义为:
@GetMapping(value = "/path/to/endpoint?fields=[\"nm\",\"label\"]")
public String hitEndpoint() {}
调试显示正在调用端点:
https://url/path/to/endpoint?fields=[%22nm%22&fields=%22label%22]
这是失败的,因为端点需要字段参数的有效 json:
https://url/path/to/endpoint?fields=[%22nm%22,%22label%22]
如何说服 GetMapping 正确提出请求?
谢谢你的帮助。
无需在 URL 中定义 Array 参数,使用 spring,您可以如下定义 API,并借助@RequestParam
注释的强大功能,您可以定义您希望在 URL 中接收数组作为参数
@RequestMapping(value = "/path-to-endpoint", method = RequestMethod.GET)
public ResponseEntity<YourResponseDTO> yourMethodName(
@RequestParam("ids") List<Long> arrayParam) {
// Return successful response
return new ResponseEntity<>(service.yourServiceMethod(requestDTO), HttpStatus.OK);
}
然后您可以使用以下 URL 调用您的GET
端点:
/path-to-endpoint?ids=1,2,3,4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.