栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

修改的地方为:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号