【问题标题】:Feign Client ignoring request paramsFeign 客户端忽略请求参数
【发布时间】:2018-01-05 17:44:32
【问题描述】:

我创建了Feign Client

@FeignClient(name = "yandex",url="${yandex.ribbon.listOfServers}")
public interface YandexMapsRestApiServiceClient {
    @RequestMapping(method = RequestMethod.GET, value = "{geoParam}")
    String  getCountryInfo(@Param("geoParam") String geoParam);
}

在控制器中我被写了:

@Autowired
private YandexMapsRestApiServiceClient client;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String test() {
   return  client.getCountryInfo("Moscow");
}

我的Applicaton.yml 看这个:

yandex:
  ribbon:
    listOfServers: https://geocode-maps.yandex.ru/1.x/?format=json&geocode=
    ConnectTimeout: 20000
    ReadTimeout: 20000
    IsSecure: true
hystrix.command.default.execution:
  timeout.enabled: true
  isolation.thread.timeoutInMilliseconds: 50000

当我尝试得到一些结果时,我得到 404 错误:

feign.FeignException: status 404 reading YandexMapsRestApiServiceClient#getCountryInfo(String); content:

在这种情况下,我在调试器中看到他feign 没有设置我的geoParam

为什么会发生这种情况以及如何解决这个问题?

【问题讨论】:

  • 尝试使用 @RequestParam 而不是 @Param

标签: spring-boot spring-cloud-netflix netflix-feign spring-cloud-feign feign


【解决方案1】:

正如Musaddique 所说,您正在混合FeignSpring 注释。使用Spring Cloud Feign(OpenFeign)时,必须使用Spring注解RequestParamFeign 注释不会被处理。

更新

要实现您的目标,您需要更改配置。 url 只能是 url 或服务名称。对 url 使用查询字符串或其他扩展会产生意想不到的结果。

将路径信息移动到RequestMapping注解,并在那里指定查询参数。

@FeignClient(name = "yandex", url="${yandex.ribbon.listOfServers}")
public interface YandexMapsRestApiServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/1.x?format=json&geocode={geoParam}")
    String getCountryInfo(@RequestParam("geoParam") String geoParam);
}

您的功能区配置如下所示:

yandex:
  ribbon:
     listOfServers: "https://geocode-maps.yandex.ru"
     ConnectTimeout: 20000
     ReadTimeout: 20000
     IsSecure: true

现在,使用您的client.getCountryInfo("moscow") 示例将得到https://geocode-maps.yandex.ru/1.x?format=json&geocode=moscow 的最终网址。

【讨论】:

  • 我正在使用所有 Spring 注释,但仍然存在请求参数未发送的问题;还有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 2018-07-22
  • 2020-01-17
  • 1970-01-01
相关资源
最近更新 更多