@FeignClient forces @GetMapping with @RequestBody to POST
Asked Answered
D

2

7

I have following REST controller with GET method that have BODY, that works fine with tests and postman

@RestController
@RequestMapping(value = "/xxx")
public class Controller {
    @GetMapping({"/find"})
    public LocalDateTime findMax(@RequestBody List<ObjectId> ids) {
        //return sth   
    }
}

but when FeignClient is used to call service, instead GET request a POST request is generated (@GetMapping annotation is ignored)

@FeignClient
public interface CoveragesServiceResource extends CoveragesService {
    @GetMapping({"/find"})
    LocalDateTime findMax(@RequestBody List<ObjectId> ids);
}

that gives an error:

Request method 'POST' not supported
Disturb answered 14/6, 2018 at 15:57 Comment(0)
B
5

GET request technically can have body but the body should have no meaning as explained in this answer. You might be able to declare a GET endpoint with a body but some network libraries and tools will simply not support it e.g. Jersey can be configured to allow it but RESTEasy can't as per this answer.

It would be advisable to either declare /find as POST or don't use @RequestBody.

Burr answered 14/6, 2018 at 16:14 Comment(0)
C
0

Solution:

Include the feign-hc5 dependency

In your application.yml enable the use of of this client:

spring:
 cloud:
  openfeign:
   httpclient:
    hc5:
     enabled: true

Note:

For HttpURLConnection (the default client) or OkHttp clients, GET requests with a body will not work. HttpURLConnection will convert such requests to POST, leading to a 405 Method Not Allowed error, while OkHttp does not support sending a body with GET requests.

Crude answered 22/8 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.