Search code examples
javaspringrestwsdl

Accessing API exposed via wsdl in spring REST based client


How can we access a wsdl of a soap project whose war is deployed on the same server, by a Rest based project using spring maven. Basically , I have to access an API that is exposed via wsdl and I have to access this API, the response than needs to be returned as json from a rest POST method. It will be like a REST post method, accepting the inputs and invoking this API (from wsdl) and manipulating the response as JSON,

I have to jump into the WebServices and Spring framework, without through knowledge. So, any help or directions to learn these things fast would be appreciated.


Solution

  • Hi I have used the following approach to implement the above requirement:
    http://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/
    1. changed the pom to add spring-ws dependency and plugin.
    2. build the classes and it generated the classes from the wsdl.
    3. changed the application xml :
         <!--Generating web sources-->
    
        <!-- Define the SOAP version used by the WSDL -->
        <bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
            <property name="soapVersion">
                <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
            </property>
        </bean>
    
        <!-- The location of the generated Java files -->
        <oxm:jaxb2-marshaller id="marshaller" contextPath="com.pb.pims.generatedsources"/>
    
        <!-- Configure Spring Web Services -->
        <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
            <constructor-arg ref="soapMessageFactory"/>
            <property name="marshaller" ref="marshaller"/>
            <property name="unmarshaller" ref="marshaller"/>
            <property name="defaultUri" value="http://localhost/HSWS/services/HSService?wsdl"/>
        </bean>
    
    4. Created the Service class;
    
    @Service
    public class HSService {
        @Autowired
        private WebServiceTemplate webServiceTemplate;
    
    
        public List<HSChild> getHSChildren(String hscode, String country,String limit) {
    
            GetHSChildren getHSChildren= new ObjectFactory().createGetHSChildren();
    
            getHSChildren.setCountry(country);
            getHSChildren.setHsCode(hscode);
            getHSChildren.setLimit(Integer.parseInt(limit));
    
            GetHSChildrenResponse response = (GetHSChildrenResponse) webServiceTemplate.marshalSendAndReceive(getHSChildren);
    
            return response.getGetHSChildrenReturn();
        }
    
    
        public static void main(String[] args) {
            ApplicationContext context = new    ClassPathXmlApplicationContext("applicationContext.xml");
            HSService hsService = context.getBean(HSService.class);
        }
    }
    
    
    So, I am able to call this aPI from the wsdl via my client. But I am always getting the values of the getGetHSChildrenReturn. hscode  and getGetHSChildrenReturn.description as null.
    
    Please find below the getGetHSChildrenReturn.class generated in  the Step1 :
    
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "getHSChildrenReturn"
    })
    @XmlRootElement(name = "getHSChildrenResponse")
    public class GetHSChildrenResponse {
    
        @XmlElement(required = true)
        protected List<HSChild> getHSChildrenReturn;
    
        public List<HSChild> getGetHSChildrenReturn() {
            if (getHSChildrenReturn == null) {
                getHSChildrenReturn = new ArrayList<HSChild>();
            }
            return this.getHSChildrenReturn;
        }
    
    Also, I verified in the service code , which we are invoking via this wsdl by putting logging, that the correct request is going and it is returning the expected response at service end. But while coming to the client, the values are set as null.
    
    Please help, what's wrong here in the client side.
    
    Thanks in advance.