FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: PathVariable annotation was empty on param 0.

我在练习微服务的时候发现了一个很奇怪的事情。在用openFeign的时候报了一个异常,花了我半天的时间,最后问题解决了。代码如下:

这个是被调用的控制层代码

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable Integer id){
        String result = paymentService.paymentInfo_ok(id);
        log.info("*******result: "+ result);
        return result;
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable Integer id){
        String result = paymentService.paymentInfo_Timeout(id);
        log.info("*******result: "+ result);
        return result;
    }
}

  下面是调用控制层的接口:

@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable Integer id);
}

  这时就会报一个错误,FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: PathVariable annotation was empty on param 0.

一般来说@PathVariable 如果是一个参数的话,是可以省略的吗?我启动被调用的控制层都没问题。然后启动调用的服务,然后报错。

之后我一点点去看,发现可能是@PathVariable的问题。

我就奇怪了,为啥会报错。当然如果把这个注解删了,能启动但是报500错误。

最后正确代码提上:

@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
    /*
    * 我发现了一个很奇怪的现象,就是如果@PathVariable后面没有加参数的话,就会报错。
    * */

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable("id") Integer id);
}

  

原文地址:https://www.cnblogs.com/LixiaoFeng1650062546/p/14040650.html