我已经从这个 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)
我究竟做错了什么?请帮助新手:-) 提前谢谢