如何以编程方式构造基于WSDL和Request XML的SOAP请求

时间:2018-11-14 17:18:31

标签: java web-services soap wsdl weblogic

有一个Web服务,从SOAPUI调用时可以正常工作。

我有一个Web服务客户端,如下面所示,在将SoapBodyElement混搭时返回错误。 (据说它在= D之前工作) 检查代码,得出的是,在调用服务时,它会接收SoapBodyElement作为参数,这就是服务失败的地方。

从WSDL中,我看到类型是anyType,因此应该构造requestbody或part(在这里我很困惑)并将其作为参数传递。

问题是,如何根据WSDL和有关正常运行请求的知识来正确生成args?

网络服务调用

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

错误

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

请求(在通过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>

1 个答案:

答案 0 :(得分:0)

解决方案很简单(找不到它)。

在这种情况下,问题在于weblogic-maven-plugin用于构建WS Client

w3lib

我刚刚将其升级到2.9.5版,并相应地更改了目标

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

,它可以恢复工作,而无需更改原始代码。