0

I am using a FeignClient and a GetMapping to interact with an external system.
My GetMapping is defined as:

   @GetMapping(value = "/path/to/endpoint?fields=[\"nm\",\"label\"]")
   public String hitEndpoint() {}

Debug shows that the endpoint is being called with:

https://url/path/to/endpoint?fields=[%22nm%22&fields=%22label%22]

Which is failing because the endpoint expects valid json for the fields parameter:

https://url/path/to/endpoint?fields=[%22nm%22,%22label%22]

How do I convince GetMapping to make the request correctly?

Thanks for any help.

bkqdad
  • 15
  • 3

2 Answers2

1

Although I think its better to pass JSON as body of a POST method to your controller. But if you insist on doing this I can propose to you 2 solutions:

First Solution

Encode your JSON array into Percent-encoding so you can send it via URL.

For example, your array will be like this:

["nm","label"] -> %5B%22nm%22%2C%22label%22%5D

I used this online tool to encode it.

Second Solution

  1. Encode your array into Base64 and GET it via URL to your controller.
  2. Decode the given Base64 string in the controller and parse it as a JSON array.
0

There is no need to define your Array parameter in the URL, using spring you can define your API as below and with the power of @RequestParam annotation you can define that you expect to receive an array as a parameter in the 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);
    }

Then you can call your GET endpoint using below URL:

/path-to-endpoint?ids=1,2,3,4
Sobhan
  • 1,280
  • 1
  • 18
  • 31
  • Thanks for your response. Unfortunately, I do not control the actual endpoint and am trying to use a feignclient to talk to it. – bkqdad Jun 27 '22 at 21:26