我一直在疯狂地试图解决这个问题,非常简短,这是我用来执行对REST端点的调用的代码:
String url = "http://localhost:5000/getMyObject";
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(factory);
String result = restTemplate.getForObject(url, String.class);
但无论我添加什么标头,它总是以连接被拒绝而告终。
堆栈跟踪代码片段:
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
...
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776)
... 74 more
设置:在Windows上测试运行,在WSL中运行应用程序
- 它通过Curl工作(在控制台和我的测试中)
- 如果测试本身启动应用程序,则它起作用
- 它通过Web浏览器工作
Curl-v:
localhost:5000/getMyObject
* Trying 127.0.0.1:5000
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /getMyObject HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.68.0
> Accept: */*
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 22 Jan 2022 10:05:31 GMT
<
* Connection #0 to host localhost left intact
{<Data>}
1条答案
按热度按时间n3h0vuf21#
SimpleClientHttpRequestFactory的一些功能不能很好地运行,我将其改为HttpComponentsClientHttpRequestFactory,现在它运行得很好。