请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

RestTemplate访问404

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

正在回答 回答被采纳积分+3

插入代码

1回答

大目 2021-04-21 16:21:59

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

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

0 回复 有任何疑惑可以回复我~
  • 提问者 公交买站票 #1
    这个问题影响不大,后面使用RestTemplate整合sentinel的时候好使了
    回复 有任何疑惑可以回复我~ 2021-04-22 14:04:23
  • 大目 回复 提问者 公交买站票 #2
    OK,解决就好哦
    回复 有任何疑惑可以回复我~ 2021-04-22 21:18:03
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信