The CXF component in the Mule ESB uses the Apache CXF library to help us create a SOAP Web Service or consume from a SOAP Web Service. In this tutorial, I will show you how to use CXF component in Mule ESB to create a SOAP Web Service.
First, I will create a new Maven project in Anypoint Studio as an example:
To create a new SOAP Web Service using the CXF component in the Mule ESB, we need to do the following:
The first step is to use the HTTP Connector in the Message Source section to expose an HTTP server that allows the client to access our SOAP Web Service.
If you guys do not know how to configure HTTP Connector, you can refer to this tutorial.
Here, I configure as follows:
Global Configuration:
With the above configuration, we will expose a SOAP Web Service at http://localhost:8081/hello, right? ????
Next, we will drag and drop the CXF component into the Message Processor in our application.
The CXF component will process the request data from the user.
The configuration for the CXF component is as follows:
To configure the CXF component, we need to configure two parts:
- The first part is Global Configuration, to configure this section, click the Add button next to the Config Reference field.
Leave the default value and click the OK button.
- The second part is that we will choose the operation that we need to perform.
Currently, the CXF component supports the following operations:
To create a new SOAP Web Service, you need to select JAX-WS service operation, other operations will be used for other purposes and I will not mention in this tutorial.
Then, the configuration of the CXF component is as follows:
There are many fields here but actually, to expose SOAP Web Service, we just need to configure the Service Class field to be enough, the values of other fields can be taken from the definition of the Service Class field. To know what we need to configure for Service Class field, first, let me briefly talk about SOAP Web Service with Apache CXF.
To expose the SOAP Web Service using Apache CXF, we need to create an interface with Apache CXF’s @WebService annotation. In this tutorial, I will create a simple interface as follows:
1 2 3 4 5 6 7 8 |
package com.huongdanjava.muleesbcxfsoapexpose; import javax.jws.WebService; @WebService public interface HelloWorldService { } |
If you know about SOAP Web Service, you will know in a SOAP Web Service, we will have one or more operations, the request data from the user will contain the operation name that they want the SOAP Web Service to execute. With Apache CXF, an operation would correspond to a method marked with the @WebMethod annotation. For example, I create an operation called hello located in HelloWorldService as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava.muleesbcxfsoapexpose; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface HelloWorldService { @WebMethod String hello(String name); } |
So, we have defined a simple interface to expose a SOAP Web Service. The value of the Service Class field in the configuration of the CXF component is the name of the interface that we have.
Our last step is to use the Mule ESB Java component to configure the HelloWorldService interface implementation class.
Here, we will implement the HelloWorldService interface as simple as this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.muleesbcxfsoapexpose; import javax.jws.WebService; @WebService public class HelloWorldServiceImpl implements HelloWorldService { @Override public String hello(String name) { return "Hello, " + name; } } |
At this point, the configuration of the Java component should look like this:
So we’ve finished the configuration. Now you can use SOAP UI to send a request to our SOAP Web Service to check the results:
kanchan
Hello Khanh,
Thank you for explaining in details. We have the same implementation in Mule 3 and now I need to migrate to 4.0. How can we expose web service developed using Apache CXF’s @WebService annotation.on Mule 4. We are using mule 4 community edition. the component CXF is no longer available in Mule 4.
Thank you again and appreciate the time and response.