by Alexander Prohorenko and Olexiy Prohorenko
Nowadays, many applications work in decentralized, distributed environments. Most of these applications need to exchange structured information with other applications. One of the best ways to do this is to use a SOAP ("Simple Object Access Protocol") protocol.
The SOAP protocol has many different implementations, but this article will focus on Apache Axis. Apache Axis is becoming popular among developers because "it has proven itself to be a reliable and stable base on which to implement Java Web services." At it‘s core, Axis is mainly a framework for constructing SOAP processors like clients and servers, but its use is not limited to this.
Another framework which is also very widely used and supposed to be a "best fit" for lightweight applications is the Spring Framework. Spring is a layered J2EE application framework, which includes, but is not limited to, the most complete lightweight container and flexible MVC Web application framework. Spring was created to address the complexity of enterprise application development.
So, what happens if you connect these two "best fit" frameworks together?
This question has been asked often—and there are many different ways to mix the two. In this article, you‘ll write a SOAP Web service using Apache Axis with classes available for configuration and initialization using the Spring Framework. Hopefully, this will give you an idea of the possiblities made available by using the two frameworks together.
You will be writing a Web service which will have a String doSomeWork(String work)
. This method is implemented by the BusinessLogic.java
interface and BusinessLogicImpl.java
class. The functionality of this method is that it can do whatever you need. Here is the sample interface:
package us.prokhorenko.springaxis; public interface BusinessLogic { public String doSomeWork(String work); }
Here‘s the code for the class, implementing the interface:
package us.prokhorenko.springaxis; public class BusinessLogicImpl implements BusinessLogic { public String doSomeWork(String work) { String ret; ret = "The work " + " is done."; return ret; } }
This code is simple and does not require any comments. The business logic can contain any methods you want.
The Web service requires you to have an interface and an implementation. First comes the interface, WebService.java
:
package us.prokhorenko.springaxis.soap; public interface WebService extends java.rmi.Remote { public String doSomeWork(String work) throws java.rmi.RemoteException; }
The interface needs to extend java.rmi.Remote
and its method can throw java.rmi.RemoteException
exception. The implementation of this interface will look a bit more complex, but I will try to vanish all your fears. So, this is the WebServiceImpl.java
class:
package us.prokhorenko.springaxis.soap; import us.prokhorenko.springaxis.BusinessLogic; import us.prokhorenko.springaxis.BusinessLogicImpl; import org.springframework.remoting.jaxrpc.ServletEndpointSupport; public class WebServiceImpl extends ServletEndpointSupport implements WebService { private BusinessLogic logic; protected final void onInit() { this.logic = (BusinessLogicImpl)getWebApplicationContext() .getBean("bizLogic"); } public String doSomeWork(String work) throws java.rmi.RemoteException { return logic.doSomeWork(work); } }
This implementation is much like the business logic (except the fact that it does not include method itself). This class extends the org.springframework.remoting.jaxrpc.ServletEndpointSupport
abstract class. This is one of the key elements of intergrating of Spring and Axis. Extending this class provides a reference to the current Spring application context—like looking up a bean or resource loading.
Using the Spring framework, initialize the BusinessLogic logic
variable, to let String doSomeWork(String work)
make a call to the method in the business logic, which will do the actual work. Next, define a private
variable for logic
and leave its initialization to the onInit()
method. This is the method that performs callbacks for custom initialization after the context has been set up. bizLogic
is the name of the bean, which defines the business logic.
The Spring configuration starts in the web.xml
file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app version="2.4"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>springaxis</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springaxis</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
This configures Spring‘s application context path and sets the mapping of *.html
to the springaxis
servlet. It will require the springaxis-servlet.xml
configuration file, which you‘ll leave empty for now. The last step is to configure the application context and beans in the applicationContext.xml
file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="bizLogic" class="us.prokhorenko.springaxis.BusinessLogicImpl" /> </beans>
deploy.wsdd
file. You don‘t need to configure too many of its options, but in case you ever need to, this file is very well documented at Apache Axis‘ Deployment (WSDD) Reference. For now, however, you will need only a few options: <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="webservice" provider="java:RPC" style="rpc" use="encoded"> <parameter name="wsdlTargetNamespace" value="urn:soap.springaxis.prokhorenko.us"/> <parameter name="className" value="us.prokhorenko.springaxis.soap.WebServiceImpl"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
Define the service name as webservice
. This name is used to deploy and undeploy an Axis service. className
defines the backend implementation class, which is us.prokhorenko.springaxis.soap.WebServiceImpl
(WebServiceImpl.java
). Use allowedMethods
to determine which methods are allowed to be exposed as Web services; in this example, you are allowing everything. At least, wsdlTargetNamespace
defines the namespace for this service, which is set to urn:soap.springaxis.prokhorenko.us
. web.xml
file: <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/soap/*</url-pattern> </servlet-mapping>
The most important thing to note is the mapping: the code maps /soap/
to Axis‘ Web services.
build.xml
is pretty standard and normally would not require any changes except for the Axis configuration: <target name="prepaxis" depends="compwar"> <echo message=" **** Preparing Axis "/> <java classname="org.apache.axis.utils.Admin" fork="true" failonerror="true" classpathref="compile.class.path" dir="${build}\WEB-INF\"> <arg value="server" /> <arg file="src/java/us/prokhorenko/springaxis/soap/deploy.wsdd" /> </java> </target>
It sets the file
option which points to the deploy.wsdd
configuration file. And you may also need to set the proper values in the properties.xml
file. Here‘s how the sample building and deploying process looks:
Buildfile: build.xml compwar: [echo] **** Compiling WAR [mkdir] Created dir: C:\white\work\Java\sa\build\WEB-INF\classes [javac] Compiling 5 source files to C:\white\work\Java\sa\build\WEB-INF\classes prepaxis: [echo] **** Preparing Axis [java] - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled. [java] <?xml version="1.0" encoding="UTF-8"?> [java] <Admin>Done processing</Admin> prepwar: [echo] **** Preparing WAR packwar: [echo] **** Packing WAR [mkdir] Created dir: C:\white\work\Java\sa\dist [war] Building war: C:\white\work\Java\sa\dist\springaxis.war all: deploy: [echo] **** Deploying WAR [copy] Copying 1 file to C:\jakarta-tomcat-5.0.30\webapps BUILD SUCCESSFUL Total time: 16 seconds
Now the application is deployed and ready to serve.
http://localhost:8080/springaxis/soap/webservice
. It calls the doSomeWork()
method and passes "TheNameOfWork"
as an argument. After building and deploying, you‘ll be able to run this client at http://localhost:8080/springaxis/SOAP.html Hopefully, this gives you a detailed and easy-to-repeat tutorial on enabling methods from business logic as Web services.
聯(lián)系客服