![](/img/trans.png)
[英]Jaxb UnMarshal Error : unexpected element (uri:“”, local:“processedSalesOrderTypeList”). Expected elements are
[英]Java-WS unmarshal error: Unexpected element “xyz. Expected elements are ”"
我有一個Java Web服務客戶端正在與遠程.Net SOAP Web服務對話。 我需要用原始XML代替參數之一,而不是打包和解壓縮相應的JAXB Java對象。
問題:
我選擇使用“ javax.xml.bind.Unmarshaller”插入原始XML。 (我不知道是否還有另一種更好的方法)。
如果使用JAXB,我驗證了該調用是否有效,並記錄了SOAP請求消息(請參見下文)。
我收到WS error: Unexpected element "requestOptions". Expected elements are "".
WS error: Unexpected element "requestOptions". Expected elements are "".
當我嘗試直接添加XML時。
正在使用JAVA CLIENT(返回有效的“ RequestOptions” JAXB對象):
private RequestOptions mkRequestOptions () throws Exception {
RequestOptions requestOptions = new com.mypackage.shwsclients.ObjectFactory().createRequestOptions ();
requestOptions.setTransactionId("007");
requestOptions.setUserName("testName");
requestOptions.setWorkflowName("testWorkflow");
requestOptions.setModuleName("testModule");
return requestOptions;
}
失敗的JAVA CLIENT(嘗試解組XML字符串失敗):
private static String theXml =
"<requestOptions>\n" +
" <WorkflowName>unmarshalTestWorkflow</WorkflowName>\n" +
" <ModuleName>unmarshalTestModule</ModuleName>\n" +
" <UserName>unmarshalTestName</UserName>\n" +
" <TransactionId>0099</TransactionId>\n" +
"</requestOptions>\n";
private RequestOptions mkRequestOptions () throws Exception {
JAXBContext context = JAXBContext.newInstance(RequestOptions.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object obj = unmarshaller.unmarshal(new StringReader (theXml)); // DIES HERE!!!!
RequestOptions requestOptions = (RequestOptions)obj;
return requestOptions;
}
成功的SOAP請求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<GetAddressData xmlns="http://myservice.com/wsdl/myservice">
<requestOptions>
<WorkflowName>testWorkflow</WorkflowName>
<ModuleName>testModule</ModuleName>
<UserName>testName</UserName>
<TransactionId>007</TransactionId>
</requestOptions>
<zipCode>90210</zipCode>
</GetAddressData>
</soapenv:Body>
</soapenv:Envelope>
自動生成的代理代碼:
//
// Generated By:JAX-WS RI 2.2.4-b01 (JAXB RI IBM 2.2.4)
//
package com.mypackage;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RequestOptions", propOrder = {
"workflowName",
"moduleName",
"userName",
"transactionId"
})
public class RequestOptions
implements Serializable
{
@XmlElement(name = "WorkflowName")
protected String workflowName;
@XmlElement(name = "ModuleName")
protected String moduleName;
@XmlElement(name = "UserName")
protected String userName;
@XmlElement(name = "TransactionId")
protected String transactionId;
...
問題已解決。
解決方案是將所有參數放入XML,然后解組整個消息。
我以為我可以解組一個論點。 相反,我需要解組整個消息-類JAXB注釋為“ @XmlRootElement”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.