在新的 openfeign 库(版本 3.1.3)中,有一个 checkin PathVariableParameterProcessor ,验证带有 @PathVariable
注释的参数应该填充 name 属性。 RequestParamParameterProcessor 中也存在类似的检查。
在 openfeign official documentation没有该规则的迹象,有一些带有命名和未命名注释的示例
在 one of the issues spring-cloud-netflix 库(据我所知是 openfeign 的前身)的人们也建议只声明参数名称。
在我的项目中,我声明了 openfeign 接口(interface),但没有显式指定参数名称(例如 @PathVariable String someId
),并且到目前为止一直有效。现在我从上述检查中得到了异常。
... org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mycompany.client.MyClient': Unexpected exception during bean creation; nested exception is
java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
...
Caused by:
java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
at feign.Util.checkState(Util.java:122)
at org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor.processArgument(PathVariableParameterProcessor.java:52)
at org.springframework.cloud.openfeign.support.SpringMvcContract.processAnnotationsOnParameter(SpringMvcContract.java:280)
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:126)
at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:193)
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:65)
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:151)
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:49)
at feign.Feign$Builder.target(Feign.java:268)
at org.springframework.cloud.openfeign.DefaultTargeter.target(DefaultTargeter.java:30)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:451)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:402)
at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:235)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1249)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1191)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
... 130 more
客户端接口(interface)api示例
@GetMapping("/{entityType}/{entityId}")
List<Document> getDocs(
@PathVariable String entityType,
@PathVariable String entityId,
@RequestParam(required = false) String ownerId,
@RequestParam(required = false) String q);
有人可以解释一下,自己写名字真的是强制性的吗?还是有其他方法可以自动化它?
最佳答案
是的,必须写入路径变量的名称。 Spring OpenFeign的官方文档中没有关于此的信息还。因此,与参数名称一起使用。 @PathVariable("someId") String someId
关于java - Feign 接口(interface)是否应该具有带有指定名称的 @PathVariable 和 @RequestParam 注释才能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74651755/