简体   繁体   中英

How to programatically construct a SOAP request based on WSDL and Request XML

There's a webservice that when called from SOAPUI works fine.

I have a webservice client, that is returning an error when mashalling a SoapBodyElement as indicated bellow. ( was said that it was working before =D ) Inspecting the code, it comes that when invoking the service, its receiving a SoapBodyElement as a parameter, and that's where the service fails.

From WSDL I see the the type is anyType so the requestbody or part (i'm confused here) should be constructed and passed as an argument.

The question is, how can I generate the the args properly based on the WSDL and the knowledge about the proper functioning request?

WebService Call

public void test() throws SOAPException {
    CCCaminhaoProxy proxyCCCaminhao = new CCCaminhaoProxy();

    DigestBO digest = BOFactory.getDigestBO();

    SOAPMessage message = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPBody body = envelope.getBody();

    Name transicao = envelope.createName("Transicao");
    Name idTransacao = envelope.createName("idTransacao");
    Name numeroAgendamento = envelope.createName("numeroAgendamento");
    Name placaCarreta = envelope.createName("placaCarreta");
    Name compartimento = envelope.createName("compartimento");
    Name faseCarregamento = envelope.createName("faseCarregamento");

    SOAPBodyElement root = body.addBodyElement(transicao);

    Random r = new Random();
    String vIdTransacao = String.valueOf(Math.abs(r.nextLong()));

    root.addAttribute(idTransacao, vIdTransacao);
    root.addAttribute(numeroAgendamento, "1035622");
    root.addAttribute(placaCarreta, "KNX0002");
    root.addAttribute(compartimento, "01");
    root.addAttribute(faseCarregamento, "01");

    SOAPElement soapElement;
    try {
        soapElement = (SOAPElement) proxyCCCaminhao.digest(root);
        digest.tratarRetornoDigest(soapElement);
    } catch (ErroComunicacaoCCCaminhaoException e) {
        logger.error(null, e);
    }       
}

ERROR

com.domain.controleacesso.integracao.cccaminhao.ErroComunicacaoCCCaminhaoException: java.rmi.RemoteException: web service invoke failed: javax.xml.soap.SOAPException:  failed to serialize class java.lang.Objectweblogic.xml.schema.binding.SerializationException: type mapping lookup failure on class=class weblogic.xml.saaj.SOAPElementImpl TypeMapping=TYPEMAPPING SIZE=3
    ENTRY 1:
     class:    java.lang.Object
     xsd_type: ['http://www.w3.org/2001/XMLSchema']:xsd:anyType
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d02f37
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d02fa3
    ENTRY 2:
     class:    java.lang.Object
     xsd_type: ['http://xml.domain.biz/2010/scoa:changestate']:stns:digestReturn
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d031c4
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d0321d
    ENTRY 3:
     class:    java.lang.Object
     xsd_type: ['http://xml.domain.biz/2010/scoa:changestate']:stns:digest
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d3dff6
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d3e04f
    ; nested exception is: 
        javax.xml.soap.SOAPException:  failed to serialize class java.lang.Objectweblogic.xml.schema.binding.SerializationException: type mapping lookup failure on class=class weblogic.xml.saaj.SOAPElementImpl TypeMapping=TYPEMAPPING SIZE=3
    ENTRY 1:
     class:    java.lang.Object
     xsd_type: ['http://www.w3.org/2001/XMLSchema']:xsd:anyType
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d02f37
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d02fa3
    ENTRY 2:
     class:    java.lang.Object
     xsd_type: ['http://xml.domain.biz/2010/scoa:changestate']:stns:digestReturn
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d031c4
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d0321d
    ENTRY 3:
     class:    java.lang.Object
     xsd_type: ['http://xml.domain.biz/2010/scoa:changestate']:stns:digest
     ser:      weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d3dff6
     deser:    weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec@1d3e04f

Request (that works when sending through SOAPUI)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:scoa="http://xml.domain.biz/2010/scoa:changestate" xmlns:chan="http://xml.domain.biz/2010/scoa/changestate">
   <soapenv:Header/>
   <soapenv:Body>
      <scoa:digest>
        <Transicao idTransacao="1541772100700" numeroAgendamento="1035622" placaCarreta="KNX0002" compartimento="01" faseCarregamento="01"/>
      </scoa:digest>
   </soapenv:Body>
</soapenv:Envelope>

WSDL

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:impl="http://xml.domain.biz/2010/scoa:changestate"
    xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:intf="http://xml.domain.biz/2010/scoa:changestate"
    targetNamespace="http://xml.domain.biz/2010/scoa:changestate">
    <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.domain.biz/2010/scoa:changestate">
            <element name="digest" type="xsd:anyType"></element>
            <element name="digestReturn" type="xsd:anyType"></element>
        </schema>
    </wsdl:types>
    <wsdl:message name="digestRequest">
        <wsdl:part element="impl:digest" name="part"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="digestResponse">
        <wsdl:part element="impl:digestReturn" name="digestReturn"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="ChangeStateWS">
        <wsdl:operation name="digest">
            <wsdl:input message="impl:digestRequest" name="digestRequest"></wsdl:input>
            <wsdl:output message="impl:digestResponse" name="digestResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ChangeState.svcSoapBinding" type="impl:ChangeStateWS">
        <wsdlsoap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http"></wsdlsoap:binding>
        <wsdl:operation name="digest">
            <wsdlsoap:operation soapAction=""></wsdlsoap:operation>
            <wsdl:input name="digestRequest">
                <wsdlsoap:body use="literal"></wsdlsoap:body>
            </wsdl:input>
            <wsdl:output name="digestResponse">
                <wsdlsoap:body use="literal"></wsdlsoap:body>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="ChangeStateWSService">
        <wsdl:port name="ChangeState.svc" binding="impl:ChangeState.svcSoapBinding">
            <wsdlsoap:address
                location="http://domain.ltd/scoa/services/ChangeState.svc"></wsdlsoap:address>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

type-mappint

<?xml version="1.0" encoding="UTF-8"?>
<wsdd:type-mapping  xmlns:wsdd="http://www.bea.com/servers/wls70"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <wsdd:type-mapping-entry   class-name="java.lang.Object"
   type="xsd:anyType"
   serializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec"
   deserializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec">
 </wsdd:type-mapping-entry>
 <wsdd:type-mapping-entry   xmlns:stns="http://xml.domain.biz/2010/scoa:changestate"
   class-name="java.lang.Object"
   type="stns:digestReturn"
   serializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec"
   deserializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec">
 </wsdd:type-mapping-entry>
 <wsdd:type-mapping-entry   xmlns:stns="http://xml.domain.biz/2010/scoa:changestate"
   class-name="java.lang.Object"
   type="stns:digest"
   serializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec"
   deserializer="weblogic.xml.schema.binding.internal.builtin.XSDAnyCodec">
 </wsdd:type-mapping-entry>
</wsdd:type-mapping>

The solution was simple (not to find it).

In this case the problem was with weblogic-maven-plugin to build the WS Client

<groupId>org.codehaus.mojo</groupId>
<artifactId>weblogic-maven-plugin</artifactId>

I just upgraded it to version 2.9.5 and changed the goal accordingly

<goals>
   <goal>clientgen9</goal>
</goals>

and it get back to work with no changes on the original code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM