2017-05-05 85 views
1

我正在開發一個實現簡單控制檯應用程序的Spring項目,該應用程序必須調用外部REST Web服務傳遞參數並從中獲取響應。爲什麼只有在使用Spring RestTemplate執行調用時,這個外部Web服務調用纔會出錯?

此WebService呼叫是:

http://5.249.148.180:8280/GLIS_Registration/6 

其中是指定ID。如果你打開瀏覽器這個地址(或捲曲工具),你會得到預期的錯誤消息:

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <sampleid>IRGC 100000</sampleid> 
    <genus>Oryza</genus> 
    <error>PGRFA sampleid [IRGC 100000], genus [Oryza] already registered for this owner</error> 
</response> 

此錯誤消息是此請求的預期的響應,我也使用捲曲正常獲取它工具來執行請求。

所以我必須從我的Spring應用程序執行這個GET請求。

要做到這一點我創建這個GETRESPONSE()方法爲RESTClient實現類:

@Service 
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class RestClient { 

    RestTemplate restTemplate; 
    String uriResourceRegistrationApi; 

    public RestClient() { 
     super(); 

     restTemplate = new RestTemplate(); 
     uriResourceRegistrationApi = "http://5.249.148.180:8280/GLIS_Registration/7"; 
    } 

    public ResponseEntity<String> getResponse() { 

     ResponseEntity<String> response = restTemplate.getForEntity(uriResourceRegistrationApi, String.class); 
     return response; 

    } 
} 

然後我把這個測試方法這個方法:

@Test 
public void singleResourceRestTest() { 
    System.out.println("singleResourceRestTest() START"); 

    ResponseEntity<String> result = restClient.getResponse(); 

    System.out.println("singleResourceRestTest() END"); 
} 

但我遇到一個非常奇怪的行爲,它發生的是:

1)對我的電話xternal web服務似乎發生了(我從Web服務日誌中看到它)。

2)web服務檢索具有所述參數值但隨後似乎不能使用它爲已完成毫無問題執行從瀏覽器或由外殼statment請求:

curl -v http://5.249.148.180:8280/GLIS_Registration/7 

但現在,調用這樣一來,我的web服務(我不能發佈的代碼,因爲它是一個WSO2 ESB流)給我這個錯誤消息:

<200 OK,<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <error>Location information not correct</error> 
    <error>At least one between &lt;genus> and &lt;cropname> is required</error> 
    <error>Sample ID is required</error> 
    <error>Date is required</error> 
    <error>Creation method is required</error> 
</response>,{Vary=[Accept-Encoding], Content-Type=[text/html; charset=UTF-8], Date=[Fri, 05 May 2017 14:07:09 GMT], Transfer-Encoding=[chunked], Connection=[keep-alive]}> 

展望Web服務日誌似乎在進行通話使用RestTemplate它使用檢索到的數據庫查詢執行數據庫查詢時遇到一些問題。

我知道它看起來非常奇怪,你可以看到:「問題是你的web服務,而不是Spring RestTemplate」。這只是部分正確,因爲我實現執行低級別的HTTP GET調用這個自定義的方法,這callWsOldStyle()(推杆到以前RESTClient實現類):使用

public void callWsOldStyle() { 
    HttpURLConnection connection = null; 
    BufferedReader reader = null; 

    try { 
     URL restAPIUrl = new URL("http://5.249.148.180:8280/GLIS_Registration/7"); 
     connection = (HttpURLConnection) restAPIUrl.openConnection(); 
     connection.setRequestMethod("GET"); 

     // Read the response 
     reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     StringBuilder jsonData = new StringBuilder(); 
     String line; 

     while ((line = reader.readLine()) != null) { 
      jsonData.append(line); 
     } 


     System.out.println(jsonData.toString()); 
    }catch(Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
    // Clean up 
     IOUtils.closeQuietly(reader); 
     if(connection != null) 
      connection.disconnect(); 
    } 
} 

這種方法,而不是RestTemplate一個能正常工作,這行:

System.out.println(jsonData.toString()); 

打印預期的結果:

<?xml version="1.0" encoding="UTF-8"?><response><sampleid>IRGC 100005</sampleid><genus>Oryza</genus><error>PGRFA sampleid [IRGC 100005], genus [Oryza] already registered for this owner</error></response> 

總結:

  • 從瀏覽器調用我的WS它的工作原理。
  • 使用cURL調用我的WS它的作品。
  • 使用我的調用我的WS callWsOldStyle()它的工作方法。
  • 使用使用方法調用我的WS RestTemplate它在我的WS接收並嘗試處理請求時發生錯誤。

那麼,這個問題的原因是什麼?我錯過了什麼?也許可以依靠一些錯誤的標題或類似的東西?

+0

RestTemplate的重點還在於允許映射到任何類。 由於看起來您正在接收位置,因此嘗試解析爲位置而不是字符串。 –

+0

@Lewis_McReu你到底意味着什麼? –

+0

您目前有這個: 'ResponseEntity response = restTemplate.getForEntity(uriResourceRegistrationApi,String。類)' 而不是字符串,使用一個類的變量對應於接收到的XML中的變量。 –

回答

0

由於皮特說您收到內部服務器錯誤(狀態碼500),所以您應該檢查此休息服務的服務器端。

在任何情況下,你可以做的resttemplate

  • 下,如果 你需要做的事情在請求
  • 爲了提取數據創建一個org.springframework.web.client.ResponseExtractor<String> 對象創建org.springframework.web.client.RequestCallback對象
  • 使用resttemplate

org.springframework.web.client.RequestCallback

public class SampleRequestCallBack implements RequestCallback 
{ 

    @Override 
    public void doWithRequest(ClientHttpRequest request) throws IOException 
    { 
    } 

} 

org.springframework.web.client.ResponseExtractor

public class CustomResponseExtractor implements ResponseExtractor<String> 
{ 
    private static final Logger logger = LoggerFactory.getLogger(CustomResponseExtractor.class.getName()); 
    @Override 
    public String extractData(ClientHttpResponse response) throws IOException 
    { 

     try 
     { 
      String result = org.apache.commons.io.IOUtils.toString(response.getBody(), Charset.forName("UTF8")); 
      if(logger.isInfoEnabled()) 
      { 
       logger.info("Response received.\nStatus code: {}\n Result: {}",response.getStatusCode().value(), result); 
      } 
      return result; 
     } 
     catch (Exception e) 
     { 
      throw new IOException(e); 
     } 
    } 
} 

REST TEMPLATE CALL

@Test 
public void testStack() 
{ 
    try 
    { 
     String url = "http://5.249.148.180:8280/GLIS_Registration/6"; 
     String response = restTemplate.execute(url, HttpMethod.GET, new SampleRequestCallBack(), new CustomResponseExtractor());; 
     logger.info(response); 
    } 
    catch (Exception e) 
    { 
     logger.error("Errore", e); 
    } 
} 

安吉洛

相關問題