使用SOAP-WS

时间:2018-05-24 16:13:56

标签: spring-boot soap jax-ws spring-ws wsdl2java

我正在尝试使用与第三方预订服务器相关的SOAP Web服务。

这是工作请求(使用SOAPUI测试):

    <?xml version="1.0" encoding="UTF-8"?>
<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:Header>
  <OGHeader transactionID="SomeID" primaryLangID="E"
   timeStamp="SomeTimeStamp" channelValidation="false"
   timeStampSpecified="true" xmlns="http://webservices.****">
   <Origin entityID="****" systemType="****" />
   <Destination entityID="***" systemType="**" />
   <Authentication>
    <UserCredentials>
     <UserName>*****</UserName>
     <UserPassword>*****</UserPassword>
     <Domain>***</Domain>
    </UserCredentials>
   </Authentication>
  </OGHeader>
 </soapenv:Header>
 <soapenv:Body>
  <FutureBookingSummaryRequest
   canHandleVaultedCreditCard="true"
   xmlns="*****.wsdl">
   <AdditionalFilters GetList="true"
    IncludePseudoRoom="false" ReservationDisposition="***"
    ReservationStatus="RESERVED">
    <ns1:HotelReference hotelCode="******"
     xmlns:ns1="http://webservices.****" />
   </AdditionalFilters>
  </FutureBookingSummaryRequest>
 </soapenv:Body>
</soapenv:Envelope>

从WSDL生成所需的包之后 我创建了我的 SOAP客户端,如下所示:

public class GloriaWClient {
    private static final Logger log = LoggerFactory.getLogger(GloriaWClient.class);

    public void getFutureBooking() {
        FutureBookingSummaryRequest futurebookingrequest = new FutureBookingSummaryRequest();
        /********** Setting the BookingFilters ****************************/
        FetchBookingFilters booking_filters = new FetchBookingFilters();
        booking_filters.setGetList(true);
        booking_filters.setIncludePseudoRoom(false);
        booking_filters.setReservationDisposition(ReservationDispositionType.***);
        booking_filters.setReservationStatus(ReservationStatusType.***);
        /************************** HotelReference ****************************/
        HotelReference hotelReference = new HotelReference();
        hotelReference.setHotelCode("***");
        booking_filters.setHotelReference(hotelReference);
        /****************************** REQUEST ********************************/
        futurebookingrequest.setCanHandleVaultedCreditCard(true);
        futurebookingrequest.setAdditionalFilters(booking_filters);

        log.info("Requesting future booking");


        try {
            SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());

                /******* Adding the SOAP Header ****/

            messageFactory.afterPropertiesSet();
            WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory);
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

            marshaller.setContextPath("com.GeneratedPackage's name");
            marshaller.afterPropertiesSet();

            webServiceTemplate.setMarshaller(marshaller);
            webServiceTemplate.setUnmarshaller(marshaller);
            webServiceTemplate.afterPropertiesSet();

            FutureBookingSummaryResponse response = (FutureBookingSummaryResponse) webServiceTemplate
                    .marshalSendAndReceive("https://***********.asmx",
                            futurebookingrequest, new WebServiceMessageCallback() {
                                @Override
                                public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
                                    try {
                                        // Setting header values
                                        OGHeader ogHeader = new OGHeader();
                                        ogHeader.setTransactionID("****");
                                        ogHeader.setPrimaryLangID("**");     
                                       ogHeader.setChannelValidation(false);        
                                        log.debug("Created ogHeader");
                                        OGHeaderAuthentication ogHeaderAuth = new OGHeaderAuthentication();
                                        OGHeaderAuthenticationUserCredentials ogHeaderAuthUserCredentials = new OGHeaderAuthenticationUserCredentials();
                                        ogHeaderAuthUserCredentials.setUserName("******");
                                        ogHeaderAuthUserCredentials.setUserPassword("****");
                                        ogHeaderAuthUserCredentials.setDomain("***");
                                        ogHeaderAuth.setUserCredentials(ogHeaderAuthUserCredentials);
                                        ogHeader.setAuthentication(ogHeaderAuth);
                                        // get the header from the SOAP message
                                        SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
                                        log.debug("Got header from the SOAP message");

                                        // create the header element
                                        ObjectFactory factory = new ObjectFactory();
                                        OGHeader futureBookingsummarySoapHeaders = factory.createOGHeader();                                    futureBookingsummarySoapHeaders.setAuthentication(ogHeaderAuth);
                                        JAXBElement<OGHeader> headers = factory                                         .createOGHeader(futureBookingsummarySoapHeaders);
                                        log.debug("Header element created");
                                        // create a marshaller
                                        JAXBContext context = JAXBContext.newInstance(OGHeader.class);
                                        Marshaller marshaller = context.createMarshaller();
                                        log.debug("Marshaller created");
                                        // marshal the headers into the specified result
                                        marshaller.marshal(headers, soapHeader.getResult());
                                    } catch (Exception e) {
                                        log.error("error during marshalling of the SOAP headers", e);
                                    }
                                }
                            });
            FutureBookingSummaryResponse msg = (FutureBookingSummaryResponse) response; 
        } catch (Exception s) {
            s.printStackTrace();
        }
    }
}

注意我确保设置合适的Body&amp;标题的属性。

执行此客户端会抛出此

  

org.springframework.ws.soap.client.SoapFaultClientException:服务器无法识别HTTP标头SOAPAction的值:。       在org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:38)       在org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:830)       在org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:624)       在org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)       在org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)       在com.lbc.migration.GloriaClient.GloriaWClient.getFutureBooking(GloriaWClient.java:104)       在com.lbc.migration.main.main(main.java:16)

任何IDEA?或任何相关的示例,以创建具有此类标题的Spring启动SOAP客户端

1 个答案:

答案 0 :(得分:1)

您缺少SOAPAction标头,如错误中所示。

  

SOAPAction是HTTP标头,它与SOAPEnvelope标头不同。

WSDL应该具有SOAPAction的定义,如

<soap1:operation style="document" soapAction="petition"

它位于SOAPEnvelope标头中的HTTP标头,,如您在请求信封中所述。

HTTP标头在TCPMONitor

中看起来像下面的内容
POST /StockQuote HTTP/1.1
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "petition"

它应该设置为 -

 webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {

public void doWithMessage(WebServiceMessage message) {
    ((SoapMessage)message).setSoapAction("http://tempuri.org/Action");
}});