0

我有一个 Java Web 服务客户端与远程 .Net SOAP Web 服务通信。我需要将原始 XML 替换为参数之一,而不是打包和解包相应的 JAXB Java 对象。

问题:

  1. 我选择使用“javax.xml.bind.Unmarshaller”来插入原始 XML。(我不知道是否有另一种更好的方法)。

  2. 如果我使用 JAXB,我验证了调用是否有效,并记录了 SOAP 请求消息(见下文)。

  3. WS error: Unexpected element "requestOptions". Expected elements are "".当我尝试直接添加 XML 时,我得到了。

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

FAILING 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;
    ...
4

1 回答 1

0

问题已解决。

解决方案是将所有参数放入 XML,然后解组整个消息。

我以为我能够解开一个论点。相反,我需要解组整个消息 - 类 JAXB 注释为“@XmlRootElement”。

于 2013-02-05T08:09:16.503 回答