Kamil W
Kamil W

Reputation: 2366

FeignClient converts GET method to POST

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?

Upvotes: 6

Views: 10958

Answers (7)

Ivan Polovyi
Ivan Polovyi

Reputation: 156

I had the same error because the name of a query param was different than the one in the request line. enter image description here When I fixed it the error was gone enter image description here

Upvotes: 2

user3593025
user3593025

Reputation: 21

I too faced this problem. and resolved it after noticing a parameter name in the path value was not having same case as it was used in @PathVariable alias name.

@RequestMapping(method = RequestMethod.GET, path = "v1/{key}/user/profiles/{parentUser}")
String getUser(@PathVariable("key") String key, @PathVariable("parentuser") String parentuser);

So if you notice in the above code snippet. the parentUser in the Path value is not matching with the name parentuser in PathVariable annotation.

During the call the client, assumes the PathVariable to be a additional parameter and tries to convert it as Gson Object.

and if the Controller at Server has another method with similar URL for e.g. PUT operation, then it will try to call that method and not the GET method.

I am not sure if the answer is relevant. But after a long analysis I found out this silly mistake and thought if it would help someone else who is in similar situation and can save the unnecessary stress and time.

thanks

Upvotes: 0

Viczai Gábor
Viczai Gábor

Reputation: 139

I faced a similar problem, using Feign and Spring Boot and using a single complex Object as the parameter. I successfully solved it, by recognizing:

  1. If one does not specify @SpringQueryMap beside @Parameter, Feign converts the parameter object to a json and puts it to the body of the request.
  2. Since GET requests with body are not really conform to http standards, the default http client used by Feign automatically converts the GET to POST. This, however can be worked around: if one adds Apache Http client 5 as dependency to the project, Feign will use it automatically instead of the java.net.http.HttpClient. And Apache's client does not changes the method, so it will stay GET. (Please note that the parameter object will still travel in the body as a json.) Simply put this to your pom.xml:
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hc5</artifactId>
        </dependency>

Spring Boot's autoconfigure feature will handle the rest.

Upvotes: 3

Willian Dougherty
Willian Dougherty

Reputation: 91

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.

Upvotes: 0

jbyler
jbyler

Reputation: 7847

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.

Upvotes: 2

Kamil W
Kamil W

Reputation: 2366

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.

Upvotes: 4

stacker
stacker

Reputation: 4475

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

Upvotes: 1

Related Questions