2014-04-28 21 views
5

Tôi đang cố gắng sử dụng dịch vụ REST với các tiện ích Spring theo hướng dẫn this nhưng tôi đang gặp sự cố với kết nối. tôi muốn truy cập với phương thức GET các dịch vụ RESTSpring RestTemplate thiết lập lại kết nối

http://date.jsontest.com/

Tôi đã viết đoạn mã này để tiêu thụ các dịch vụ

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.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.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); 

    } 

} 

Ứng dụng này ném chồng ngoại lệ sau đây

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$1.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$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.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 

Tôi đã thử gọi REST với hai kết nối mạng khác nhau, do đó, đó không phải là sự cố kết nối mạng; hơn nữa nếu tôi truy cập dịch vụ từ trình duyệt, tôi có thể nhận phản hồi chính xác.

Điều gì có thể là vấn đề?

Cảm ơn bạn trước

+0

Bạn đã thêm thư viện jackson, có vẻ như có vấn đề khi cố gắng trích xuất dữ liệu, vì url đang hoạt động tốt – Koitoer

+0

Xin chào @Koitoer, cảm ơn bạn đã trả lời. Tôi đã thêm các phần bổ trợ jackson-core, jackson-annotations và jackson-databind (ver 2.2.3) vào tệp pom.xml của tôi. – gvdm

+0

Và điều đó giải quyết được vấn đề? hoặc tồn tại – Koitoer

Trả lời

4

Giải quyết. Sự cố này bị ràng buộc với kết nối mạng. Mạng tôi thuộc về tường lửa và proxy. bây giờ tôi đang cố gắng để hiểu làm thế nào để làm cho mọi thứ được thực hiện với các mạng tường lửa (đó là công ty của tôi một).

+0

Tôi gặp lỗi tương tự. "ClientAbortException" trên máy chủ, nhưng "SocketException" trên máy khách. Bạn có thể cho tôi biết những gì bạn phải thay đổi trong tường lửa để xóa lỗi này không? – Pytry

+0

Xin chào @Pytry. Tôi không ở cùng một công ty và bây giờ làm việc với các công nghệ khác nên tôi không thể tái tạo vấn đề và cho bạn biết giải pháp là gì:/nhưng nói chung nếu mạng của bạn được ủy quyền thì bạn phải cấu hình công ty proxy trong máy khách REST của bạn để cho phép nó thực hiện các yêu cầu thông qua máy chủ proxy – gvdm

Các vấn đề liên quan