How to consume third party WSDL services in Spring MVC

空扰寡人 提交于 2019-12-02 00:38:10

Finally I'm able to access Third services.

This is my method to access service

   public void createSoapActionCallBack(ValidateCardRequest validateCardRequest) {

        //This is used to send header message
        SoapActionCallback actionCallBack=new SoapActionCallback(soapAction);
        try{

            actionCallBack = new SoapActionCallback(ResponseConstants.SOAPACTION_DEFAULT_URL) {
            public void doWithMessage(WebServiceMessage msg) {
                    SoapMessage smsg = (SoapMessage)msg;                
                    SoapHeader soapHeader = smsg.getSoapHeader();

                    try{
                        //To send header message
                        StringSource headerSource = new StringSource("<UserCredentials xmlns='URL'>\n" +
                                        "<userid>"+"ABCD"+"</userid>\n" +
                                        "<password>"+"ABCD"+"</password>\n" +
                                        "</UserCredentials>");
                        Transformer transformer = TransformerFactory.newInstance().newTransformer();
                        transformer.transform(headerSource, soapHeader.getResult());

                        smsg.setSoapAction(soapAction);
                    }catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                }; 
               validateCardResponse = (FVValidateCardResponse) webServiceTemplate.marshalSendAndReceive(URL, validateCardRequest, actionCallBack);  

            } catch (Exception e) {
                e.printStackTrace();
            }       
}

This is my configuration xml file:

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="soapVersion">
            <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
        </property>
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<!-- If we want to use contextPath then we mush create ObjectFactory class in the described Package-->
        <!-- <property name="contextPath" value="com.waleteros.firstviewmodel" /> -->

        <property name="classesToBeBound">
            <list>
                <value>com.waleteros.firstviewmodel.FVValidateCardRequest</value>
                <value>com.waleteros.firstviewmodel.FVValidateCardResponse</value>               
            </list>
        </property>
    </bean>

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory" />
        <property name="marshaller" ref="marshaller"></property>
        <property name="unmarshaller" ref="marshaller"></property>
        <property name="messageSender">
            <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender"/>            
        </property>
        <!-- <property name="defaultUri" value="https://www.firstviewcorp.com/dbbapplications/ServicesSS/Selfservice.asmx?wsdl"/> -->
    </bean>

Create pojo's according to your xmls Here is example

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "CardUpdateResponse",namespace="http://www.corecard.com/Prepaid")
public class FVCardUpdateResponse {

    @XmlElement(name="CARDUPDATE_RET", namespace="http://www.corecard.com/Prepaid")
    private CARDUPDATE_RET response;
    //Getters and Setters   

    public static class CARDUPDATE_RET{

        @XmlElement(name = "ACCOUNTNUMBER", namespace="http://www.corecard.com/Prepaid")
        private String AccountNumber;

        @XmlElement(name = "ResCode", namespace="http://www.corecard.com/Prepaid")
        private String ResCode;

        @XmlElement(name = "ResErrorCode", namespace="http://www.corecard.com/Prepaid")
        private String ResErrorCode;

        @XmlElement(name = "ResErrorMsg", namespace="http://www.corecard.com/Prepaid")
        private String ResErrorMsg;
        //Getters and Setters
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!