将 @RequestLine 与 Feign 结合使用
Using @RequestLine with 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);
}
我得到异常
Caused by: java.lang.IllegalStateException: Method getLinksForTrack not annotated with HTTP method type (ex. GET, POST)
知道为什么吗?
我没想到这会奏效。
@RequestLine
是核心 Feign 注释,但您使用的是 Spring Cloud @FeignClient
,它使用 Spring MVC 注释。
Spring 已经创建了他们自己的 Feign Contract
以允许您使用 Spring 的 @RequestMapping
注释而不是 Feigns。您可以通过 including a bean of type feign.Contract.Default
in your application context.
禁用此行为
如果您正在使用 spring-boot
(或使用 Java 配置的任何东西),将其包含在 @Configuration
class 中应该会重新启用 Feign 的注释:
@Bean
public Contract useFeignAnnotations() {
return new Contract.Default();
}
您的 @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")
我有一个工作的 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);
}
我得到异常
Caused by: java.lang.IllegalStateException: Method getLinksForTrack not annotated with HTTP method type (ex. GET, POST)
知道为什么吗?
我没想到这会奏效。
@RequestLine
是核心 Feign 注释,但您使用的是 Spring Cloud @FeignClient
,它使用 Spring MVC 注释。
Spring 已经创建了他们自己的 Feign Contract
以允许您使用 Spring 的 @RequestMapping
注释而不是 Feigns。您可以通过 including a bean of type feign.Contract.Default
in your application context.
如果您正在使用 spring-boot
(或使用 Java 配置的任何东西),将其包含在 @Configuration
class 中应该会重新启用 Feign 的注释:
@Bean
public Contract useFeignAnnotations() {
return new Contract.Default();
}
您的 @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")