Reputation: 2366
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
Reputation: 156
I had the same error because the name of a query param was different than the one in the request line. When I fixed it the error was gone
Upvotes: 2
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
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:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hc5</artifactId>
</dependency>
Spring Boot's autoconfigure feature will handle the rest.
Upvotes: 3
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
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
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.x
applications.
Upvotes: 4