Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Thursday, 11 July 2013

Implementing Web Services with Spring and Axis

In the past, I wrote a post on how to implement Web Services using JAX-WS on Glassfish, and Apache Axis. In this post I will describe how to implement Web Services using the Spring framework and Apache Axis. The spring framework uses JAX-RPC API to help implement and access SOAP-WSDL based Web Services. The main components required for implementing and accessing Web Services in Spring are:
  • JaxRpcPortProxyFactoryBean: This is a proxy factory for proxies that communicate with backend Web Services.
  • ServletEndPointSupport: This is the base class for Web Service End Points. A Web Service end point is a class that will be exposed as a Web Service.

Creating the Web Service

The example contains a simple Web Service that echoes back the request message to the client. Follow these steps to imlement the Web Service
  1. Start with a dynamic web project in Eclipse.
  2. The Service Interface: The following is the code for the Service Interface. Nothing new here.
    package service;

    public interface ISpringWS {

    public String sayHello(String message);

    }
    ISpringWS.java
  3. The Service Implementation: The following is the code for the Service Implementation. Again, it's a simple POJO.
    package service;

    public class SpringWS implements ISpringWS {

    public String sayHello(String message) {
    System.out.println("sayHello:" + message);
    return "You said '" + message + "'";
    }
    }
    SpringWS.java
  4. The Service Endpoint: Here is the code for the Service Endpoint, followed by an explanation
    package service;

    import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

    public class SpringWSEndPoint extends ServletEndpointSupport implements ISpringWS {
    private ISpringWS springWS;

    protected void onInit() {
    this.springWS = (ISpringWS) getWebApplicationContext().getBean("springWS");
    }

    public String sayHello(String message) {
    return springWS.sayHello(message);
    }
    }
    SpringWSEndPoint.java

    To implement Web Services using the Spring framework, a service endpoint class has to be written for each service. The service endpoint generally delegates the requests to the Spring-managed beans which implement the actual web service. The service endpoint, however, is not managed by Spring, but by the Web Service too (Axis in our case). Also note that the Service Endpoint implements the Service Interface, since this class acts is the interface to the Web Service.
  5. The server configuration: In order to use Axis as the deployment tool for the web service, you have to add a service section to the Axis server-config.wsdd file. Here is the file in full.
    <?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="SpringWS" provider="java:RPC">
    <parameter name="allowedMethods" value="*" />
    <parameter name="className" value="service.SpringWSEndPoint" />
    </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>
    WEB-INF/server-config.wsdd
  6. The spring application context: Here is the applicationContext.xml used for the example
    <?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="springWSEndpoint" class="service.SpringWSEndPoint"></bean>
    <bean id="springWS" class="service.SpringWS"></bean>
    </beans>
    WEB-INF/applicationContext.xml
  7. The Web deployment descriptor
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>WSSpring</display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>axis</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    <load-on-startup>5</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>axis</servlet-name>
    <url-pattern>/axis/*</url-pattern>
    </servlet-mapping>
    </web-app>
    WEB-INF/web.xml
    The Axis servlet definition enables the Axis servlet to make the service available under the given port name.
  8. Jar files: Here is a list of the jar files used for this example
    axis.jarAvailable for download at Apache Axis website
    commons-discovery.jarAvailable with the Spring framework with dependencies download, or here.
    commons-logging.jarAvailable with the Spring framework with dependencies download, or here.
    jaxrpc.jarAvailable with the Spring framework with dependencies download.
    log4j-1.2.13.jarAvailable with the Spring framework with dependencies download, or here.
    saaj.jarAvailable with the Spring framework with dependencies download.
    spring.jarNo need to say where.
    wsdl4j-1.5.1.jarAvailable with the Spring framework with dependencies download.
  9. Deploy and Test: Deploy the application in Weblogic 9.2. You can test the service at the URL
    http://localhost:7001/WSSpring/axis/SpringWS?wsdl

The Web Service Client
  1. Start with a dynamic web project in Eclipse and include all the above jar files.
  2. Service Interface: The service interface can be generated using any of the tools that create Java classes using the WSDL file. For this example, simply copy the ISpringWS.java file into the client application.
    package service;

    public interface ISpringWS {

    public String sayHello(String message);

    }
    ISpringWS.java
  3. The client: The client is a simple Java class, with the service as a member. This will be injected by the spring framework.
    package client;

    import service.ISpringWS;

    public class SpringWSClient {
    ISpringWS springWS;

    public String callService() {
    return springWS.sayHello("Hello");
    }

    public ISpringWS getSpringWS() {
    return springWS;
    }

    public void setSpringWS(ISpringWS springWS) {
    this.springWS = springWS;
    }

    }
    SpringWSClient.java
  4. The Client Servlet:
    package servlets;

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import client.SpringWSClient;

    public class WSSpringClientServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    public WSSpringClientServlet() {
    super();
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
    SpringWSClient sender = (SpringWSClient) ctx.getBean("springWSClient");
    String result = sender.callService();
    response.getWriter().println(result);
    response.getWriter().close();
    }
    }
    WSSpringClientServlet.java
  5. The Application context: You can see the definition of the JaxRpcPortProxyFactoryBean here. This class was described briefly earlier in the post.
    <?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="jaxRpcProxy" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean">
    <property name="serviceFactoryClass">
    <value>org.apache.axis.client.ServiceFactory</value>
    </property>
    <property name="wsdlDocumentUrl">
    <value>http://localhost:7001/WSSpring/axis/SpringWS?wsdl</value>
    </property>
    <property name="namespaceUri">
    <value>http://localhost:7001/WSSpring/axis/SpringWS</value>
    </property>
    <property name="serviceName">
    <value>SpringWSEndPointService</value>
    </property>
    <property name="portName">
    <value>SpringWS</value>
    </property>
    <property name="serviceInterface">
    <value>service.ISpringWS</value>
    </property>

    </bean>
    <bean id="springWSClient" class="client.SpringWSClient">
    <property name="springWS" ref="jaxRpcProxy" />
    </bean>

    </beans>
    WEB-INF/applicationContext.xml

    The service interface used here is a plain Java interface. Using the definition this way will turn service invocations into dynamic JAX-RPC calls (using JAX-RPC's Dynamic Invocation Interface). Another way to implement this is to have the Service interface extend java.rmi.Remote, and have a definition of portInterface.
  6. The web deployment descriptor:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>WSSpringClient</display-name>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
    <description></description>
    <display-name>WSSpringClientServlet</display-name>
    <servlet-name>WSSpringClientServlet</servlet-name>
    <servlet-class>servlets.WSSpringClientServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>WSSpringClientServlet</servlet-name>
    <url-pattern>/WSSpringClientServlet</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
    <welcome-file>index.html</welcome-file>

    </welcome-file-list>
    </web-app>
    WEB-INF/web.xml

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...