Consume jax-ws webservice using wsdl in Mule Project

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

 

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.

 

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:

 

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

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.