Expose a Liferay Service as a Web Service
April 26th, 2010 by Bert WillemsTags:Java, Liferay, Spring, Web Services
In my previous article I showed you how you can implement a reusable Liferay Service without using Ext or the Service Builder utility. In this article we will take it one step further and expose the service we have created as web service again without using Ext or the Service Builder.
Liferay exposes the services based on Axis using a separate web application called tunnel-web. We will hook into tunnel-web so our service will be exposed in exactly the same way Liferay services are externalized. Let’s get started.
0. Project Setup
We will pick up from where we ended in the previous article. I have you haven’t followed the previous article you can download the initial project setup here. Please take a minute to familiarize yourself with the project structure.
We need to add a new dependencies to the pom of the service contract library:
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-kernel</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-impl</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
1. Add the Service Classes
Ok lets get down to business. The first step is to create the web service class in the service contract library. Create a new class called HelloWorldServiceSoap in the package nl.devatwork.hello.world.service.http and copy the following code into it:
package nl.devatwork.hello.world.service.http;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import nl.devatwork.hello.world.service.HelloWorldServiceUtil;
import java.rmi.RemoteException;
public class HelloWorldServiceSoap {
private static Log _log = LogFactoryUtil.getLog(HelloWorldServiceSoap.class);
public static String sayHello(String name)
throws RemoteException {
String result;
try {
result = HelloWorldServiceUtil.sayHello(name);
} catch (Exception e) {
_log.error(e, e);
throw new RemoteException(e.getMessage());
}
return result;
}
}
This class wraps SOAP calls and maps them to the appropriate service call.
You can now build the service contract library by executing the following command in the /hello-world-service/ folder:
mvn clean package
Copy the resulting JAR file (/hello-world-service/target/hello-world-service-1.0.0-SNAPSHOT.jar) to the /lib/ext folder of your application server.
2. Apply Glue
The final step is a bit of handwork. We need to manually register the new service to the tunnel-web application. Open the server-config.wsdd located in the deploy directory/tunnel-web/WEB-INF folder of your application server. Add the following code just above the end of the XML:
<service name="Portlet_DevAtWork_HelloWorldService" provider="java:RPC" style="rpc" use="encoded"> <parameter name="wsdlTargetNamespace" value="urn:http.service.world.hello.devatwork.nl"/> <parameter name="wsdlServiceElement" value="HelloWorldServiceSoapService"/> <parameter name="wsdlServicePort" value="Portlet_DevAtWork_HelloWorldService"/> <parameter name="className" value="nl.devatwork.hello.world.service.http.HelloWorldServiceSoap"/> <parameter name="wsdlPortType" value="HelloWorldServiceSoap"/> <parameter name="typeMappingVersion" value="1.2"/> <operation xmlns:operNS="urn:http.service.world.hello.devatwork.nl" name="sayHello" qname="operNS:sayHello" soapAction=""> <parameter xmlns:tns="http://www.w3.org/2001/XMLSchema" qname="name" type="tns:string"></parameter> </operation> <parameter name="allowedMethods" value="sayHello"/> </service>
This will register our service within tunnel-web.
Start your application server and navigate to http://127.0.0.1:8080/tunnel-web/axis. Here you will find a list of all the services currently exposed by Liferay. In the list you will find our service (Portlet_DevAtWork_HelloWorldService). You can view the WSDL by clicking on the link behind the service.
That wasn’t to bad now was it? If you are to lazy to copy the code you can download the complete project here.
I hope you learned how you can expose services as web services in Liferay. In the upcoming article I will show you how you can add security to the web service.
That is it for now. Let me know if you have any comments or questions.