2012-09-01 34 views
0

我目前正在從普通xml轉換一些Web服務。我已經在soapUI中加載它們,創建了一個客戶端端口和模擬服務,並且請求響應正常運行。所以現在我試圖創建/將XML複雜類型對象轉換爲Java,但我失敗了。將web服務xml對象轉換爲java類

在soapUI的我有這個作爲響應:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://client.serviceweb.xxx"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <cli:versionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <result xsi:type="java:ResultatVersion" xmlns:java="java:xxx.serviceweb"> 
      <messagexxx xsi:type="xsd:string">message</messagexxx> 
      <resultatxxx xsi:type="xsd:int">1</resultatxxx> 
      <version xsi:type="java:Version"> 
       <numero xsi:type="xsd:string">1.0.0</numero> 
      </version> 
     </result> 
     </cli:versionResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

不知道如何將其轉換成一個Java對象?看看原始的xml,有一個包含字段消息和結果的父對象,但我不理解版本部分。

這個怎麼樣?

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xxx="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://client.serviceweb.xxx"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <cli:testResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <result xsi:type="java:ResultatImage" xmlns:java="java:xxx.serviceweb.xxx.xxx"> 
      <messagexxx xsi:type="xxx:string">message</messagexxx> 
      <resultatxxx xsi:type="xxx:int">result</resultatxxx> 
      <image xsi:type="xxx:base64Binary">cid:1325182441595</image> 
     </result> 
     </cli:testResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我已經有一個我以前使用,並已經工作,但無法弄清楚如何調用上述樣品工作JAXB客戶端。

我有:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}resultatImage> 

好像反應不是映射到我已經創建了ResultatImage類。

有什麼想法?

我如何發送請求並解除響應。

try { 
    JAXBContext jc = JAXBContext 
      .newInstance("com.ipiel.response"); 
    Unmarshaller u = jc.createUnmarshaller(); 
    client = new MyClient(httpClient, targetHost, u); 
    client.test(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

public void test() { 
    String url = "/xxxClientsPort/test"; 
    HttpPost httpPost = new HttpPost(url); 

    HttpResponse response = httpClient.execute(targetHost, httpPost); 
    log.info("response: " + response); 
    if (response.getStatusLine().getStatusCode() == 200) { 
     HttpEntity respEntity = response.getEntity(); 
     if (respEntity != null) { 
      InputStream instream = respEntity.getContent(); 
      try { 
       ResultatImage responseEntity = (ResultatImage) unmarshaller 
         .unmarshal(instream); 
       /* 
       * FileWriter writer = new FileWriter(new 
       * File("c:\\tmp\\output")); IOUtils.copy(instream, writer, 
       * "UTF-8"); String theString = writer.toString(); 
       * writer.flush(); writer.close(); 
       * System.out.println(theString); 
       */ 
      } finally { 
       instream.close(); 
      } 
     } 
    } 
} 

package com.ipiel.response具有Resultat和ResultatImage類,它們都標記爲@XmlRootElement。此外,它包含了ObjectFactory使用WSDL標記爲@XmlRegistry

感謝,
czetsuya

回答

0

這裏的主要問題是,我使用錯誤版本的Axis在SoapUI內部生成了java類。我正在嘗試使用Axis2,但後來發現Web服務實際上在Axis1上運行。因此,如果您遇到類似問題,請首先檢查Web服務版本和客戶端庫是否相同。

0

您加載網絡服務的SOAP UI。 Wsdl包含該類的模式。
對於轉換WSDL到Java類,您可以使用Eclipse插件
wsdl2java plugin

,或者你可以使用Maven插件,像

<groupId>org.apache.axis2</groupId> 
<artifactId>axis2-wsdl2code-maven-plugin</artifactId> 

<groupId>org.apache.cxf</groupId> 
<artifactId>cxf-codegen-plugin</artifactId> 
+0

嗨,感謝您的回覆。我知道wsdltojava,但我不想創建客戶端存根。我正在使用JAXBContext和Unmarshaller。我已經添加了上面發送請求的代碼並解除響應。 – czetsuya