对于 nested exception is java.net.ConnectException问题【已解决】

2023-05-16

当在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,"操作失败");
        }
    }

修改的地方为:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对于 nested exception is java.net.ConnectException问题【已解决】 的相关文章

随机推荐