Tuesday, October 4, 2016

Constructing 'Contract-Last' web-service using MuleSoft.

In the previous blog, we discussed about what contract is and how to build contract first service using MuleSoft.

In this blog, we will see about the method of constructing contract-last web-service.

What is 'Contract-Last' Scenario?

When 2 systems are to be integrated and both systems are with their components already built in are just completed it.  They were earlier used for internal business process and the same needs to be exposed as a web-service for the external vendor needs to access it. In these scenarios, we will go with this approach.

MuleSoft uses Spring and CXF frameworks internally, to create stubs and publish the web services without any manual efforts in Mule Anypoint studio. They are created for you automatically.

The below steps are needed to create a web-service in Mule Anypoint Studio:

1. Develop 2 POJO Java classes one for request format and another one for response.
2. Develop a POJO Java class for web-service interface with @WebService annotation.
2. Configure CXF object with the necessary parameters.
3. Access the WSDL which is internally constructed automatically by Mule.
4. Optionally, add any additional functionalities to be implemented.

Step-1: Develop POJO Classes:

We will take a simple use case for demo purpose.  The input will be the name of the user and the response will be to add Greeting text 'Hallo ' as the response.

Let us create a simple Java POJO classes for both request & response as below -

package wsdlfirst;

public class Employee {

String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Now, we need to create an interface for an actual web-service stub:

package wsdlfirst;

import javax.jws.WebService;

@WebService
public interface IService {

public Employee greetEmployee(Employee name);


}

Note the annotation @WebService which internally helps to create the necessary artifacts with the help of Spring framework.

We now need to implement the web-service interface 'IService' as below:

package wsdlfirst;

public class GreetService implements IService{

@Override
public Employee greetEmployee(Employee name)
{
name.setName("Hallo " + name.getName());
return name;
}

}

All set.  Now we will get into Mule flow as below:

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="service" doc:name="HTTP Listener Configuration"/>

    <spring:beans>
        <spring:bean id="greetService" class="wsdlfirst.GreetService" scope="singleton"/>
    </spring:beans>

    <flow name="webserviceFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/greetUser" doc:name="HTTP"/>
        <cxf:jaxws-service doc:name="AddressService" serviceClass="wsdlfirst.IService"/>
            <component  doc:name="Java">
                <spring-object bean="greetService" />
            </component>
    </flow>


The first part is used to create a simple java object greetService.  The key part is to create a web-service CXF component with the webservice interface.

Take a look at the follow up with the component tag where we mention the object to be used for web service response.

The Mule Flow looks as below -

We can execute the SOAP service using Soap UI as below:



Hope you enjoyed going through this blow.  Should you have any questions, please feel free to reach out to me @ sivakumar.th@gmail.com 




1 comment: