2013-10-07 25 views
1

我已生成使用JAXB從這個XSD文件javax.xml.bind.UnmarshalException:

<xs:schema xmlns:tns="http://testwork/" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://testwork/"> 
    <xs:element name="sayHelloWorldFrom" type="tns:sayHelloWorldFrom"/> 
    <xs:element name="sayHelloWorldFromResponse" type="tns:sayHelloWorldFromResponse"/> 
    <xs:complexType name="sayHelloWorldFrom"> 
     <xs:sequence> 
      <xs:element name="arg0" type="xs:string" minOccurs="0"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="sayHelloWorldFromResponse"> 
     <xs:sequence> 
      <xs:element name="return" type="xs:string" minOccurs="0"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

歸類這是生成的類

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "sayHelloWorldFrom", namespace = "http://testwork/", propOrder = { 
    "arg0" 
}) 
public class SayHelloWorldFrom { 

    protected String arg0; 

    /** 
    * Gets the value of the arg0 property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getArg0() { 
     return arg0; 
    } 

    /** 
    * Sets the value of the arg0 property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setArg0(String value) { 
     this.arg0 = value; 
    } 

} 

我一樣有這樣

SOAP消息
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testwork/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <tes:sayHelloWorldFrom> 
     <!--Optional:--> 
     <arg0>?</arg0> 
     </tes:sayHelloWorldFrom> 
    </soapenv:Body> 
</soapenv:Envelope> 

我想將此消息格式化爲類SayHelloWorldFrom,這裏是我的代碼示例

public void unmarshalSoapRequest(InputStream is) throws JAXBException { 
     JAXBContext js = JAXBContext.newInstance(SayHelloWorldFrom.class); 
     Unmarshaller unmarshaller = js.createUnmarshaller(); 
     SayHelloWorldFrom sayHelloWorldFrom = (SayHelloWorldFrom) unmarshaller.unmarshal(is); 

但是我有一個像

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

我在做什麼錯在Tomcat中記錄一個錯誤?請幫助新手:-) Thann in advnance

+0

以下內容將有所幫助:http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html –

回答

0

您試圖使用對SOAP沒有任何認識的JAXB上下文來解組包含SOAP特定信息的流。它只能解開SOAP請求的中間部分。

<tes:sayHelloWorldFrom> 
    <!--Optional:--> 
    <arg0>?</arg0> 
    </tes:sayHelloWorldFrom> 

你不應該寫unmarshalSoapRequest方法。通常情況下,您需要創建一個實現Web服務接口的類,或者編寫WSDL並從WSDL生成代碼,而不是從XSD生成代碼。

相關問題