在Feign中使用@RequestLine

时间:2015-05-01 10:23:57

标签: java netflix-feign

我有一个有效的Feign界面定义为:

@FeignClient("content-link-service")
public interface ContentLinkServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/{trackid}/links")
    List<Link> getLinksForTrack(@PathVariable("trackid") Long trackId);

}

如果我将其更改为使用@RequestLine

@FeignClient("content-link-service")
public interface ContentLinkServiceClient {

    @RequestLine("GET /{trackid}/links")
    List<Link> getLinksForTrack(@Param("trackid") Long trackId);

}

我得到了例外

  

引起:java.lang.IllegalStateException:方法getLinksForTrack未使用HTTP方法类型注释(例如GET,POST)

任何想法为什么?

3 个答案:

答案 0 :(得分:20)

我不希望这种情况发挥作用。

@RequestLine是一个核心Feign注释,但您使用的是使用Spring MVC注释的Spring Cloud @FeignClient

答案 1 :(得分:8)

Spring创建了自己的Feign Contract,允许您使用Spring的@RequestMapping注释而不是Feigns。您可以including a bean of type feign.Contract.Default in your application context禁用此行为。

如果您正在使用spring-boot(或使用Java配置的任何内容),在@Configuration类中包含此内容应该重新启用Feign的注释:

@Bean
public Contract useFeignAnnotations() {
    return new Contract.Default();
}

答案 2 :(得分:-1)

您的@RequestMapping值看起来还不错,但是您可能应该考虑对其进行稍微重写:

 @GetMapping(value = "/{trackid}/links")
 List<Link> getLinksForTrack(@PathVariable(name = "trackid") Long trackId);

由于与您相同的错误,我没能成功使@RequestLine工作。

@ReactiveFeignClients Contract.Default()也会产生以下错误:

java.lang.IllegalStateException: Method MyClient#doStuff(String,String) not annotated with HTTP method type (ex. GET, POST)
Warnings:
- Class MyClient has annotations [Component, ReactiveFeignClient, Metadata] that are not used by contract Default
- Method doStuff has an annotation GetMapping that is not used by contract Default

,应固定为:

var MyClient = WebReactiveFeign.builder()
        .contract(new ReactiveContract(new SpringMvcContract()))
        .target(MyClient, "http://example.com")