SpringCloud中的Feign组件远程调用接口时PathVariable annotation was empty on param 0的异常解决

IllegalStateException: PathVariable annotation was empty on param 0异常解决

一.异常现象

我在利用SpringCloud进行微服务开发时,使用feign组件进行远程调用,结果产生了如下异常信息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.yyg.platform.web.service.api.CacheService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1818) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1266) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:260) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at .....
	org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:253) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:282) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:262) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 43 common frames omitted
......

Process finished with exit code 1

根据异常信息可以看到,主要是有这么一个异常信息:

IllegalStateException: PathVariable annotation was empty on param 0.

二.异常原因

从异常信息中可以看到,主要是说是PathVariable注解有问题!也就是@PathVariable注解的第0个值为空!

下面是我在声明Feign接口方法时候,使用@PathVariable注解的接口方法:

@GetMapping("/user/{id}")
public User get(@PathVariable Long id);

可以看到我的path路径部分只有一个id变量,那么说在“was empty on param 0”,也就是说id值没有取到!

三.解决办法

将@PathVariable修改为@PathVariable(value="id")的写法,明确带有value="id"!

@GetMapping("/user/{id}")
public User get(@PathVariable(value="id") Long id);

修改之后重新启动,问题解决!

 

你可能感兴趣的:(异常解决)