Java Spring RestTemplate 连接重置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23346115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring RestTemplate connection reset
提问by gvdm
I'm trying to consume a REST service with Spring facilities following thisguide but I'm having a problem with the connection. I want to access with GET method the REST service
我正在尝试按照本指南使用 Spring 设施使用 REST 服务,但我遇到了连接问题。我想使用 GET 方法访问 REST 服务
I've written this piece of code to consume the service
我写了这段代码来使用服务
package pack;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.Hymanson.annotation.JsonAnyGetter;
import com.fasterxml.Hymanson.annotation.JsonAnySetter;
import com.fasterxml.Hymanson.annotation.JsonInclude;
import com.fasterxml.Hymanson.annotation.JsonProperty;
import com.fasterxml.Hymanson.annotation.JsonPropertyOrder;
public class RestUtilityTest {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"time",
"milliseconds_since_epoch",
"date"
})
public class DateTime {
@JsonProperty("time")
private String time;
@JsonProperty("milliseconds_since_epoch")
private Integer milliseconds_since_epoch;
@JsonProperty("date")
private String date;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("time")
public String getTime() {
return time;
}
@JsonProperty("time")
public void setTime(String time) {
this.time = time;
}
@JsonProperty("milliseconds_since_epoch")
public Integer getMilliseconds_since_epoch() {
return milliseconds_since_epoch;
}
@JsonProperty("milliseconds_since_epoch")
public void setMilliseconds_since_epoch(Integer milliseconds_since_epoch) {
this.milliseconds_since_epoch = milliseconds_since_epoch;
}
@JsonProperty("date")
public String getDate() {
return date;
}
@JsonProperty("date")
public void setDate(String date) {
this.date = date;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
@Test
public void getTest()
{
RestTemplate restTemplate = new RestTemplate();
DateTime datetime = restTemplate.getForObject("http://date.jsontest.com", DateTime.class);
assertTrue(datetime != null);
}
}
The application throws the following exception stack
应用程序抛出以下异常堆栈
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://date.jsontest.com": Connection reset; nested exception is java.net.SocketException: Connection reset
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:524)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:472)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
at pack.RestUtilityTest.getTest(RestUtilityTest.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:55)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:510)
... 26 more
I tried the REST call with two different network connections so that's not a network connection problem; moreover if I visit the service from a browser I can get the response correctly.
我尝试使用两个不同的网络连接进行 REST 调用,因此这不是网络连接问题;此外,如果我从浏览器访问该服务,我可以正确获得响应。
What can be the problem?
可能是什么问题?
Thank you in advance
先感谢您
采纳答案by gvdm
Solved. The problem is bound to the network connection. The network I belong is firewalled and proxied. now I'm trying to understand how to make things done with the firewalled network (it's my company one).
解决了。问题与网络连接有关。我所属的网络有防火墙和代理。现在我试图了解如何使用防火墙网络(这是我公司的网络)完成工作。