Social Icons

Pages

Tuesday, March 4, 2014

Apache Axis 2 + Struts 2 + Spring Framework (Maven Base Project)

Overview

This post will show you how to integrate Apache Axis 2 with Existing Struts 2+ Spring Project.
I presume that you already have Struts 2 + Spring Project!

Apace Axis 2 is one of the most popular Java Web Service Framework and I am now showing you about applying this framework to our web project!

This article applies to: 

 Apache Axis 1.6.2, Apache Struts 2, Core Spring Framework 3++

Sample of the project source code can be download here!

Step By Step Tutorial

Step 1. Adding "Maven Dependency" to your existing project ("pom.xml")

  
     org.apache.axis2
     axis2
     ${org.apache.axis2.version}
  
  
     org.apache.axis2
     axis2-spring
     ${org.apache.axis2.version}
  
  
   org.apache.axis2
   axis2-transport-http
   ${org.apache.axis2.version}
  
  
   org.apache.axis2
   axis2-transport-local
   ${org.apache.axis2.version}
  
  
   org.apache.axis2
   axis2-soapmonitor-servlet
   ${org.apache.axis2.version}
  



Step 2.  Modifying your "web.xml" by adding "AxisServlet"



   AxisServlet
     
        org.apache.axis2.transport.http.AxisServlet
     
     1

     
   AxisServlet
         /axis/*
 

Step 3, Adding the "excludePattern" to "struts.xml" to support the above Axis Servlet Path.


.....


......




Step 4, Adding Apache Axis 2 Framework to your project.




What we need to do?
  • Download Apache Axis 2 Binary Distribution here if you don't have one!
  • Extract the web archive file ("axis2.war" :  located at .".\axis2-1.6.2\dist\axis2.war")
    • Copy "conf" folder from "axis2" web archive folder ("...\axis2\WEB-INF\conf") and then place it to your "WEB-INF" folder.
  • Create "modules, pojos and services" directories under  your "...\WEB-INF\..." folder
  • Modifying "axis2.xml" (...\WEB-INF\conf\axis2.xml) to support your own  web service path ("servicePath" property)
    
    axis

Note: the default service path is "services"

Step 5, Web Service Implementation

"HelloWs.java" (Simplest Web Service!)

package com.kovitad.services;
public class HelloWs {
 public String sayHello(final String name) {
  if (null == name) {
   return "Hello";
  }
  return "Hello, " + name + "!";
 }
}


"ProductWebServiceExample.java" (Simplest Spring Bean Web Service)
package com.kovitad.services;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Component;
import com.kovitad.domain.Product;

@WebService(serviceName="SearchProductWs")
@SOAPBinding(style=Style.RPC)
@Component(value="productSearchingService")
public class ProductWebServiceExample {
 com.opensymphony.xwork2.util.logging.Logger logger = com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(ProductWebServiceExample.class);
 private List<  product> products = new ArrayList<>();
 
 @PostConstruct
 public void init(){
  products.add(new Product("Product A", "http://kovitad.com/A.jgp", null));
  products.add(new Product("Product B", "http://kovitad.com/B.jgp", null));
  products.add(new Product("Product C", "http://kovitad.com/B.jgp", null));
  logger.info("Done !");
 }
 
 public List<  product> listAll(){
  return products;
 }
}

Step 6, Engaging Your Web Services

  • Adding Web Service Configuration file ("services.xml") to your project.
    • Create  "HelloWs" folder under your web service repository (".../WEB-INF/services/...")
      • Create "META-INF" folder and then place service configuration file ("services.xml") to engage your specific web service.
      • Service Configuration Template can be found at  "...\axis2-1.6.2\samples\quickstartaxiom\resources\META-INF\services.xml"
Example of Service Configuration ("services.xml") for "HelloWS" (General Java Class)


    
        Simplest Web Service!
    
    
        
        
    
    
    com.kovitad.services.HelloWs





Example of Service Configuration (services.xml) for "ProductWebServiceExample" (Spring Bean)

 
  Exporting Spring Bean Web Service!
 
 
  
  
 
 
 org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
 
 productSearchingService
 



Step 7, Testing it!


1 comment:

 
Blogger Templates