RestTemplate访问404

来源:3-15 编写用户微服务与内容微服务-1

公交买站票

2021-04-18

08:26:48.343 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://localhost:8080/users/1
08:26:48.359 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
08:26:48.391 [main] DEBUG org.springframework.web.client.RestTemplate - Response 404 NOT_FOUND
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
	at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
	at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:338)
	at com.nijvelshare.contentcenter.service.ShareService.main(ShareService.java:24)


    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> entity = restTemplate.getForEntity(
                "http://localhost:8080/users/1",
                String.class
        );
        System.out.println(entity.getBody());
    }

单独进入地址是可以访问的,不知道为什么出现404

写回答

1回答

大目

2021-04-21

从你的代码来看没啥问题。

http://localhost:8080/users/1 这个API在浏览器能访问吗?

0
2
大目
回复
公交买站票
OK,解决就好哦
2021-04-22
共2条回复

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3085 学习 · 1324 问题

查看课程