我不知道我在做什么错,但是每次假装客户端将声明为get的方法转换为post类型。
@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;
}
此设置应针对以下请求进行解决:
GET http://localhost:8114/service/clients?someValue=foo&someEnum=bar
但是每次我得到这个结果:
{
"timestamp": 1542378765498,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/service/clients"
}
但是,当我以这种方式进行操作时,效果很好:
@RequestMapping(method = GET, value = "/clients?someValue=foo&someEnum=bar")
Client getClients();
我正在使用spring-cloud-starter-feign 1.2.7.RELASE
版,其中包含feign-core/sl4fj/hystrix/ 9.3.1
版,但我也在10.1.0版上进行了测试,结果相同。
我应该怎么做才能解决此问题?
答案 0 :(得分:1)
正如已经讨论过的HERE:feign 9.3.1确实支持@QueryMap和POJO,您需要使用Map,尝试更新为feign 9.7或10.0.1
答案 1 :(得分:1)
我遇到了Spring @FeignClient
的一个实例,该实例出于另一个原因将GET请求转换为POST。就我而言,被调用的REST API使用HTTP查询参数。调用此API的Feign客户端方法的参数中带有@QueryParam
的参数(即javax.ws.rs.QueryParam)。 Feign显然无法识别此批注,因此使用该方法参数作为请求正文中的表单发布参数(并将请求方法转换为POST),而不是将其用作查询参数。
解决方法是将javax.ws.rs.QueryParam
注释替换为org.springframework.web.bind.annotation.RequestParam
。
此案是使用spring-cloud-openfeign-core-2.2.5.RELEASE发生的。
答案 2 :(得分:0)
在我的项目中,我使用spring-cloud-dependencies
和Camden.SR7
版本,其中包含9.3.1
伪装版本,当前最新版本是Finchley.RELEASE
,其中包含伪装9.7
而且我看到它专用于spring-boot 2.x.x
,但是我的整个基础架构(config / eureka服务器)都在1.5.x
上运行,因此会产生下一个问题。我看了一下伪装的github文档,发现@Param
注释可能有用,但是当我在带有3个参数的方法中使用它时,它会抛出异常Method has too many Body parameters~
。最终,spring的注释@RequestParam
可以作为解决方法,但是我没有找到任何可以组合这些注释的信息来源。
@RequestMapping(method = GET, value = "/clients")
Client getClients(@RequestParam("someValue") String someValue, @RequestParam("someEnum") String someEnum);
我没有发现spring-cloud-dependencies
版本包含9.7
伪装,并且专门用于spring-boot 1.5.x
应用程序。