初学Spring Cloud踩得坑之org.springframework.web.client.HttpClientErrorException: 404 null异常处理

异常代码

org.springframework.web.client.HttpClientErrorException: 404 null
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:332) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]

 

服务提供者的端口为:8081   

服务调用者的端口为:8088

  在服务调用者中的controller如下: 


@RestController
@RequestMapping("consumer")

public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;



    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id) {

    String url = "http://user-service/user"+ id;
     
    User user = restTemplate.getForObject(url, User.class);
    return user;
    }
}

出现异常的原因:我在写路径的时候 String url = "http://user-service/user"+ id;    user的后面少了个/导致出现这个错误,只要把/加上就能正常调用,这个错也是由于粗心导致的,也希望能够帮助和我一样自学Spring Cloud的人,让他们少走一些弯路

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值