RestTemplate访问报404,但是浏览器和Postman是可以访问的

来源:3-16 编写用户微服务与内容微服务-2

mottoyin

2025-03-12

图片描述

Idea日志:

11:37:19.835 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://localhost:8080/users/1
11:37:19.840 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
11:37:19.853 [main] DEBUG org.springframework.web.client.RestTemplate - Response 404 NOT_FOUND
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found: "<!DOCTYPE html><EOL><html lang="en"><EOL><head><EOL><meta charset="utf-8"><EOL><title>Error</title><EOL></head><EOL><body><EOL><pre>Cannot GET /users/1</pre><EOL></body><EOL></html><EOL>"
	at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:113)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:168)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
	at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:334)
	at com.imooc.contentcenter.service.share.ShareService.main(ShareService.java:31)

Process finished with exit code 1

测试的main方法

测试的main方法
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        // 用HTTP GET方法去请求,并且返回一个对象
//        ResponseEntity<String> response = restTemplate.getForEntity(
//                "http://localhost:8080/users/1", String.class);
//        System.out.println(response.getBody());

        String forObject = restTemplate.getForObject(
                "http://localhost:8080/users/1",
                String.class
        );
        System.out.println(forObject);
    }

百思不得其解,还请老师看看

写回答

1回答

mottoyin

提问者

2025-03-12

将原来的Mac的JDK从22换成了zulu JDK8后问题解决,DeepSeek和Grok3都没有给出可能是JDK的问题

1
0

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

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

3129 学习 · 1327 问题

查看课程