当在springcloud遇到这类问题时,需要排查控制器中的路径是否写对,由于我报错的项目是一个模拟客户端访问查询业务功能,下面是我的源代码(报错):

    public static final String PAYMENT_URL = "http://localhost:8001";
    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id")Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get"+id,CommonResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else{
            return new CommonResult(444,"操作失败");
        }
    }

      在访问这一网址时,报错I/O error on GET request for \"http://localhost:8080/payment/get/1\": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect"

        由于可以保证查询业务是没有问题的,经过仔细检查,发现是在这一句有问题:

ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);

        重点在

可以发现,get后应该有一个/,将后面要传入的查询值分隔,所以报错,改完以后的代码应该是:

    public static final String PAYMENT_URL = "http://localhost:8001";
    @GetMapping("/consumer/payment/getForEntity/{id}")ja
    public CommonResult<Payment> getPayment2(@PathVariable("id")Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else{
            return new CommonResult(444,"操作失败");
        }
    }

修改的地方为:

 所以在遇到此类问题时,一定要注意是否访问的路径写错了!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐