Java - 在Spring Boot中使用AXIS更改SOAP请求格式

时间:2018-06-11 12:06:29

标签: java spring-boot soap axis soap-client

我试图在Spring Boot中使用SOAP Web服务。并使用Axis生成类。

我正在从客户端发送soap请求,并且在转到服务器时正在更改请求的格式。

请查找客户端发送请求和服务器接收请求,如下所示:

SOAP Client sending Request:

      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prim="http://..../..Services">
        <soapenv:Header />
        <soapenv:Body>
          <prim:UserList>
            <prim:XMLRequest>
              <prim:Header>
                <prim:MessageID>1</prim:MessageID>
                <prim:CorrelationID>1</CorrelationID>
              </prim:Header>
            </prim:XMLRequest>
          </prim:UserList>
        </soapenv:Body>
      </soapenv:Envelope>

SOAP Server receiving Request:

      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Body>
          <UserList xmlns="hhttp://..../..Services">
            <XMLRequest>
              <header>
                <MessageID>1</MessageID>
                <CorrelationID>1</CorrelationID>
              </header>
            </XMLRequest>
          </UserList>
        </soapenv:Body>
      </soapenv:Envelope>

尝试使用以下代码致电:

public  UserListResponse UserListService(UserList request)
            throws RemoteException, ServiceException {


        UserListRequest xmlRequest = new UserListRequest();

        Header reqHeader = request.getXMLRequest().getHeader();

        Header header = new Header();

        header.setCorrelationID(reqHeader.getCorrelationID());
        header.setMessageID(reqHeader.getMessageID());

        xmlRequest.setHeader(header);

        return soapStub.UserList(xmlRequest);

    }

Axis Serializer&amp; deSerializer代码如下:

// Type metadata
private static org.apache.axis.description.TypeDesc typeDesc =
    new org.apache.axis.description.TypeDesc(Header.class, true);

static {
    typeDesc.setXmlType(new javax.xml.namespace.QName("http://..../..Services", "Header","prim"));

    org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
    elemField.setFieldName("messageID");
    elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "MessageID","prim"));
    elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
    elemField.setMinOccurs(0);
    elemField.setNillable(false);
    typeDesc.addFieldDesc(elemField);
    elemField = new org.apache.axis.description.ElementDesc();
    elemField.setFieldName("correlationID");
    elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "CorrelationID","prim"));
    elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
    elemField.setMinOccurs(0);
    elemField.setNillable(false);
    typeDesc.addFieldDesc(elemField);


}

/**
 * Return type metadata object
 */
public static org.apache.axis.description.TypeDesc getTypeDesc() {
    return typeDesc;
}

/**
 * Get Custom Serializer
 */
public static org.apache.axis.encoding.Serializer getSerializer(
       java.lang.String mechType, 
       java.lang.Class _javaType,  
       javax.xml.namespace.QName _xmlType) {
    return 
      new  org.apache.axis.encoding.ser.BeanSerializer(
        _javaType, _xmlType, typeDesc);
}

/**
 * Get Custom Deserializer
 */
public static org.apache.axis.encoding.Deserializer getDeserializer(
       java.lang.String mechType, 
       java.lang.Class _javaType,  
       javax.xml.namespace.QName _xmlType) {
    return 
      new  org.apache.axis.encoding.ser.BeanDeserializer(
        _javaType, _xmlType, typeDesc);
}

任何人都可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:0)

我不确定,为什么你在关注XML名称空间前缀的位置。

逻辑上的XML A

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prim="http://..../..Services">... <prim:UserList>...</prim:UserList> </soapenv:Body> </soapenv:Envelope>

和XML B

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">... <UserList xmlns="hhttp://..../..Services"> </UserList> </soapenv:Body> </soapenv:Envelope>

你的用例

是一样的。除了少数字符串字符之外没有区别。

对于XML A,名称空间是在根级别定义的, 因此,子项需要使用本地名称前缀来标识(如果没有冲突的\同名节点,可能会被跳过,这似乎是您的情况),它属于名称空间soapprim

在XML B的情况下,命名空间在parent node(UserList)级别定义,因此可以避免所有子节点具有localname前缀。即Defining a default namespace for an element saves us from using prefixes in all the child elements.

我希望能回答你的问题。

有关详细信息,请参阅XML Namespace