假设客户忽略请求参数

时间:2017-07-29 21:10:59

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

我创建了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

enter image description here

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

1 个答案:

答案 0 :(得分:1)

正如Musaddique所述,您正在混合FeignSpring注释。使用Spring Cloud Feign(OpenFeign)时,必须使用Spring注释RequestParam。 <{1}}注释将不会被处理。

更新

要实现您的目标,您需要更改配置。 Feign的{​​{1}}应仅为网址或服务名称。使用查询字符串或其他扩展名会产生意外结果。

将路径信息移动到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")的最终网址。