javax.xml.bind.UnmarshalException:

时间:2013-10-07 13:54:37

标签: exception jaxb unmarshalling

我已使用此xsd文件中的JAXB生成了分类

<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;
    }

}

我有一条像这样的肥皂信息

<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);

但我在Tomcat日志中遇到错误,如

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

我做错了什么?请帮助新手:-) Thanx in advnance

1 个答案:

答案 0 :(得分:0)

您正在尝试使用对SOAP一无所知的JAXB上下文来解组包含SOAP特定信息的流。它只能解组SOAP请求的中间部分。

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

您不应该编写unmarshalSoapRequest方法。通常,您要么创建一个实现Web服务接口的类,要么编写WSDL并从WSDL生成代码而不是从XSD生成代码。