2017-02-03 75 views
2

我正在處理的當前項目涉及從一個Java應用程序調用一些Web服務。 Web服務託管在運行在虛擬化Linux機器上的payara/glassfish服務器上。 Web服務從兩個不同的遺留系統返回數據,一個基於SQLServer數據庫,另一個基於FoxPro數據庫。Webservice和Unmarshalling異常

有時,webservice將返回包含xml版本1.0中不允許的值(字節)的數據,並且應用程序將引發解組異常,即響應中的無效字符(0x2)。 因爲我無法控制從數據庫中提取的數據,所以我需要找到一種方法來過濾/替換違規字符,以便應用程序可以使用這些數據。

我確實可以訪問web服務代碼,所以如果需要的話,我可以對服務和客戶端進行更改。 我的確在某處讀過xml版本1.1允許某些控制字符,但我不確定如何升級該版本,或者甚至我會這樣做。

對此提出建議?

回答

1

像本教程(https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter6/custom_marshalling.html),你也許可以從界面MessageBodyReader實施readFrom這樣使自定義解組:

Object readFrom(Class<Object>, Type genericType, 
        Annotation annotations[], MediaType mediaType, 
        MultivaluedMap<String, String> httpHeaders, 
        InputStream entityStream) 
         throws IOException, WebApplicationException { 

     try { 
     JAXBContext ctx = JAXBContext.newInstance(type); 
     StringWriter writer = new StringWriter(); 
     IOUtils.copy(inputStream, writer, encoding); 
     String theString = writer.toString(); 
     // replace all special characters 
     theString = theString.replaceAll("[\u0000-\u001f]", ""); 
     return ctx.createUnmarshaller().unmarshal(theString); 
     } catch (JAXBException ex) { 
     throw new RuntimeException(ex); 
     } 
    } 
+0

試圖使用您的示例代碼,並在調用解組,我得到由於String不是一個易於理解的參數,因此出現錯誤。 – kpenrose

+0

更正了參數問題,現在讀者正常工作。似乎我也需要在web服務端做些事情,因爲服務器偶爾會扼殺數據。這方面的錯誤似乎有點不同,因爲在對編組人員的調用中有一個超時錯誤。 – kpenrose