Let’s say you have a service in your Spring web application and this service need to be accessible via web services. So, how to expose your service? In this post, I will show you how to expose a servlet-based web services using JAX-RPC.
As an example, below is our service that need to be exposed. We will use a simple service that have a method to return the response as an object (MyResponse).
// MyService.java
public interface MyService {
public MyResponse getInfo(String value);
}
// MyServiceImpl.java
public class MyServiceImpl implements MyService{
public MyResponse getInfo(String value) {
return new MyResponse("1", value);
}
}
Based on Spring Reference, Spring provides a convenience base class for JAX-RPC servlet endpoint implementations, which is ServletEndpointSupport. So, to expose our service, we need to create a class that will extend Spring’s ServletEndpointSupport class and implement our business logic inside, usually delegating the call to the business layer (which means our implementation class).
// JaxRpcMyService.java
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
public class JaxRpcMyService extends ServletEndpointSupport implements MyService{
private MyService myService;
protected void onInit() {
this.myService = (MyService) getWebApplicationContext().getBean("myService");
}
public MyResponse getInfo(String value) {
return myService.getInfo(value);
}
}
Take note that inside this JaxRpcMyService class, we have 1 delegate method; getInfo( ). This is the only class that we need to expose our service. Simple right?
Ok, now let’s move to configurations. First, you need to add AxisServlet in your web.xml.
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
Then, you need to have a configuration for Axis (server-config.wsdd) to setup the endpoint. Take a look at <service name=”MyService”>. You need to define your className, beanMapping for all your related beans and namespace (optional). That is the configuration for your service. If you want to know more about Axis configuration, you can refer on Axis website.
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<parameter name="adminPassword" value="admin" />
<parameter name="sendXsiTypes" value="true" />
<parameter name="sendMultiRefs" value="true" />
<parameter name="sendXMLDeclaration" value="true" />
<parameter name="axis.sendMinimizedElements" value="true" />
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session" />
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request" />
<parameter name="extension" value=".jwr" />
</handler>
</requestFlow>
</globalConfiguration>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" />
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />
<service name="AdminService" provider="java:MSG">
<parameter name="allowedMethods" value="AdminService" />
<parameter name="enableRemoteAdmin" value="false" />
<parameter name="className" value="org.apache.axis.utils.Admin" />
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<service name="MyService" provider="java:RPC">
<parameter name="allowedMethods" value="*" />
<parameter name="className" value="JaxRpcMyService" />
<beanMapping qname="imsWs:MyResponse" xmlns:imsWs="urn:ImsWebservice" languageSpecificType="java:MyResponse" />
<namespace>http://ImsWebservice/</namespace>
</service>
<service name="Version" provider="java:RPC">
<parameter name="allowedMethods" value="getVersion" />
<parameter name="className" value="org.apache.axis.Version" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</requestFlow>
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder" />
</responseFlow>
</transport>
</deployment>
By this point, you had setup everything you need for your web service. You can run your web application and go to
http://localhost:8080/<context_name>/webservice/MyService?wsdl to see the wsdl.
Thank you.
-HADZRUL-