java 将 @RequestLine 与 Feign 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29985205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:14:28  来源:igfitidea点击:

Using @RequestLine with Feign

javanetflix-feign

提问by nickcodefresh

I have a working Feign interface defined as:

我有一个工作的 Feign 接口定义为:

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

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

}

If I change this to use @RequestLine

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

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

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

}

I get the exception

我得到了例外

Caused by: java.lang.IllegalStateException: Method getLinksForTrack not annotated with HTTP method type (ex. GET, POST)

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

Any ideas why?

任何想法为什么?

回答by Alex Wittig

I wouldn't expect this to work.

我不希望这会奏效。

@RequestLineis a core Feign annotation, but you are using the Spring Cloud @FeignClientwhich uses Spring MVC annotations.

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

回答by Michael K

Spring has created their own Feign Contractto allow you to use Spring's @RequestMappingannotations instead of Feigns. You can disable this behavior by including a bean of type feign.Contract.Defaultin your application context.

Spring 已经创建了自己的 FeignContract以允许您使用 Spring 的@RequestMapping注释而不是 Feigns。您可以通过在应用程序上下文中包含类型 beanfeign.Contract.Default来禁用此行为。

If you're using spring-boot(or anything using Java config), including this in an @Configurationclass should re-enable Feign's annotations:

如果您正在使用spring-boot(或任何使用 Java 配置的东西),将它包含在一个@Configuration类中应该重新启用 Feign 的注释:

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