This tutorial shows how we can consume service when we are given a wsdl file. We have seen how to create webservice and consume webservice in Mule Project but earlier we have not consume webservice from wsdl file rather we had created new mule flow for consuming webservice.

Please go through the tutorial create jax-ws webservice before reading below.
Prerequisites

Mule Studio 3.5.0
Mule 3.4.1
JDK 1.6
WSDL file
Maven 2.x

WSDL file

<definitions targetNamespace="http://service.roytuts.com/" name="HelloImplService">
<types>
    <xsd:schema>
        <xsd:import namespace="http://service.roytuts.com/" schemaLocation="http://localhost:8888/jax-ws-ssl/hello?xsd=1"/>
    </xsd:schema>
</types>
    <message name="sayHello">
        <part name="parameters" element="tns:sayHello"/>
    </message>
    <message name="sayHelloResponse">
        <part name="parameters" element="tns:sayHelloResponse"/>
    </message>
    <portType name="Hello">
        <operation name="sayHello">
            <input wsam:Action="http://service.roytuts.com/Hello/sayHelloRequest" message="tns:sayHello"/>
            <output wsam:Action="http://service.roytuts.com/Hello/sayHelloResponse" message="tns:sayHelloResponse"/>
        </operation>
    </portType>
    <binding name="HelloImplPortBinding" type="tns:Hello">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="sayHello"><soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="HelloImplService">
        <port name="HelloImplPort" binding="tns:HelloImplPortBinding">
            <soap:address location="http://localhost:8888/jax-ws-ssl/hello"/>
        </port>
    </service>
</definitions>

 

Step1. First create a Mule project called jax-ws-external in Mule Studio. If you do not know how to create a project then you can find that in Mule related other tutorials.
Step 2. We need to generate the client or stub from the wsdl file before invoking the service. We may generate the clients using the maven plugin or we can also generate using wsimport command from cmd prompt in Windows System. I will generate client using command. So I run the below command in cmd prompt.

<project root directory> wsimport -keep -d src/main/java http://localhost:8888/jax-ws-ssl/hello?wsdl

Open the cmd prompt. Go to the project root directory in Mule workspace. Note the few things in the command:

wsimport – generate clients from wsdl
-keep – keep the generated source files otherwise you will see generated class files
-d src/main/java – diretory where the client java files will be generated
http://localhost:8888/jax-ws-ssl/hello?wsdl – wsdl location

Once the command gets executed you will see the below files are generated in the package – in.webtuts.service

Hello.java
HelloImplService.java
ObjectFactory.java
package-info.java
SayHello.java
SayHelloResponse.java

Step 3. Create the mule flow file with the following configurations. You can also create the mule flow using the visual editor.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
    xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <flow name="jax-ws-external" doc:name="jax-ws-external">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8084" path="jax-ws-external/msg" doc:name="HTTP" />
        <cxf:jaxws-client operation="sayHello" doc:name="SOAP"
            enableMuleSoapHeaders="true" clientClass="in.webtuts.service.HelloImplService"
            port="HelloImplPort" />
        <http:outbound-endpoint address="http://localhost:8888/jax-ws-ssl/hello" />
        <object-to-string-transformer doc:name="Object to String" />
    </flow>
</mule>

 

We have few things to mention here

<http:inbound-endpoint/>
HTTP endpoint which takes input from the user and gives response, hence exchange-pattern=“request-response”
host and port – where the server is running
path – is the requested input from the user

<cxf:jaxws-client/>
SOAP component used for consuming SOAP based webservices
operation – look at the wsdl file for operation name
enableMuleSoapHeaders – by default true
clientClass – need to pass the client class name because we are consuming the service
port – which port to listen to

<http:outbound-endpoint/>
address – need to pass the service full address or location

<object-to-string-transformer/>
is used to render the correct format data. HTTP outbound endpoint sends payload in XML format and expects to receive response in XML as well. However, SOAP client unmarshals XML content and sends plain String (returned from sayHello method). So, let’s turn the content-type into text/plain in order to avoid browser rendering issues.

Step 4. Now run the application. If application successfully starts up then go to the browser and type url http://localhost:8084/jax-ws-external/msg

You will get the output – Hello, jax-ws-external/msg

You can also test the service in java main class as follows:

package in.webtuts.service.test;

import in.webtuts.service.Hello;
import in.webtuts.service.HelloImplService;

public class HelloClient {
    public static void main(String[] args) throws Exception {
        HelloImplService service = new HelloImplService();
        Hello hello = service.getHelloImplPort();
        System.out.println(hello.sayHello("Soumitra"));
    }
}

 

Thanks for your reading. Please do not forget to leave a comment.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *