FeignClient converts GET method to POST

Viewed 5447

I don't know what I'm doing wrong, but each time feign client converts method declared as get to post type.

@FeignClient(name = "my-service", url = "http://localhost:8114", path = "service")
public interface MyServiceClient {

    @RequestMapping(method = GET, value = "/clients")
    Client getClients(@QueryMap MyPojo pojo);
}

@Getter
@Setter
public class MyPojo {

    @NotNull
    private String someValue;
    @NotNull
    private SomeEnum someEnum;
}

This setup should be resolved to this request:
GET http://localhost:8114/service/clients?someValue=foo&someEnum=bar

But each time I'm getting this result:

{
  "timestamp": 1542378765498,
  "status": 405,
  "error": "Method Not Allowed",
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
  "message": "Request method 'POST' not supported",
  "path": "/service/clients"
}

However it works fine when I do it in this way:

@RequestMapping(method = GET, value = "/clients?someValue=foo&someEnum=bar")
Client getClients();

I'm working on spring-cloud-starter-feign 1.2.7.RELASE version which contains, feign-core/sl4fj/hystrix/ 9.3.1 version, but I also tested it on 10.1.0 version, with this same result.

What should I do to resolve this issue?

4 Answers

In my project I use spring-cloud-dependencies with Camden.SR7 version which contains 9.3.1 feign version, at current time the newest version is Finchley.RELEASE which contains feign 9.7 and I see it's dedicated for spring-boot 2.x.x, but my whole infrastructure (config/eureka server) runs on 1.5.x so it produces next issues. I took a look at github documentation for feign, and I discovered @Param annotation might be helpful, but when I use it in method with 3 arguments, it throws exception Method has too many Body parameters~. Finally annotation @RequestParam from spring works as workaround, but I didn't found any source of information that we can combine these annotations.

@RequestMapping(method = GET, value = "/clients")
Client getClients(@RequestParam("someValue") String someValue, @RequestParam("someEnum") String someEnum);

I didn't found spring-cloud-dependencies version which contains 9.7 feign and it's dedicated for spring-boot 1.5.xapplications.

As already been discussed HERE: feign 9.3.1 does support @QueryMap with A POJO, you need to use a Map, try to update to feign 9.7 or 10.0.1

I encountered an instance of Spring's @FeignClient converting a GET request into POST for a different reason. In my case, the REST API being invoked uses an HTTP query parameter. The Feign client method to invoke this API had a parameter annotated with @QueryParam (i.e. javax.ws.rs.QueryParam) for this query parameter. Feign apparently does not recognize this annotation, and so used that method parameter as a form post parameter in the request body (and turned the request method into a POST) instead of using it as a query parameter.

The fix was to replace the javax.ws.rs.QueryParam annotation with org.springframework.web.bind.annotation.RequestParam.

This case occurred using spring-cloud-openfeign-core-2.2.5.RELEASE.

In my case, I was using @Param to pass my request parameters to my GET endpoint. I don't know why, but apparently Feign convert it to a POST request.

I solve my problem replacing the @Param by @QueryMap

Sample:

@RequestLine("GET")
MyObjResponse findByObjByIdAndNameUsingRequestParams(@QueryMap Map<String, String> queryMap);

Now Feign send a correct GET http method.

Related