javaresturlfeign

How correctly send a URL in feign client as PathVariable?


I send a request, response contains URL to the next page. I need call this url, if it exist.

`@GetMapping(value = "{nextUri}")
SearchResponse getNextPage(@PathVariable("nextUri") String nextUri);`

But when i try to call this feign client method, i receive an error. My URL contains invalid characters, for example:

Correct url: search?view=full&per_page=10

Incorrect url in logs after call feign client method: search%3Fview%3Dfull%26per_page%3D10

How can i deny replacing that characters: '=', '?', '&'

I'd try using some decoders, encoders, string.replace, but its doesnt help

Error in logs: FeignException$NotFound: [404 Not Found] during [GET] to [search%3Fview%3Dfull%26per_page%3D10]


Solution

  • Inspired by this (How can I change the feign URL during the runtime?), you can try to change the signature of the feign client method to:

    @GetMapping
    SearchResponse getNextPage(URI absoluteNextUri);
    

    And build the absolute URI in the client caller as:

    URI absoluteNextUri = URI.create(baseNextUri + nextUri);
    

    where baseNextUri is the prefix that (I suppose) is already put in the annotations of the feign client, just check if you need some slashes / in the middle of concatenation. In this way, what is stored in the nextUri string will be used by the feign client with no escape changes.