Social Icons

Pages

Monday, March 10, 2014

Apache CXF with Spring Framework and Struts 2 Tutorial

Overview


This article will show you how to develop Java Web Services by using Apache CXF. I assume that you have existing Struts 2 + Spring Framework Web Application. 

The sample of Java Source Code of this article can be downloaded here!

This article applies to
Apache CXF 2.3.9, Struts 2 and Spring Framework Version 3.

Step by Step Tutorial

Step 1, Adding "Maven" Dependency

You might check your dependency tree before adding any  library by using the following command,

"mvn dependency:tree"...





 org.apache.cxf
 cxf-rt-frontend-jaxws
 ${cxf.version}


 org.apache.cxf
 cxf-rt-transports-http
 ${cxf.version}


 org.apache.cxf
 cxf-rt-bindings-soap
 ${cxf.version}


Step 2, Adding CXF Servlet

You may require to add CXF Servlet Path to register CXF service path with your current Servlet Container.

 CXFServlet
 
  org.apache.cxf.transport.servlet.CXFServlet
 
 1


 CXFServlet
 /cxf/*



Step 3, Adding this CXF Servlet path in Struts configuration file (struts.xml)

.

 
  
   
    
     login.jsp
    
   
  
 



Step 4, Implementing JEE "JAX-WS" Web Services

JAX-WS is the standard library of Java Enterprise Edition 6, you just need to add "WebService" annotation to config your web service.

@WebService
  • "Name" : default is the name of Service Interface
  • "targetNamespace" : default is package name
  • "serviceName" : default is the name of Service Implementation
  • "wsdlLocation" : URL of this web service
  •  "portName": It indicates the name of the endpoint where the service is published. It directly maps to a name attribute of the <wsdl:port> element in the WSDL document.
The following example is the Java Source Code that we are using for exporting Spring Bean as the Java Web Services.

package com.kovitad.services;

import java.util.ArrayList;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import com.kovitad.domain.Product;

@WebService(serviceName="SearchProductWs")
@SOAPBinding(style=Style.RPC)
@Component(value="productSearchingService")
public class ProductWebServiceExample {
 
 Logger logger = LoggerFactory.getLogger(ProductWebServiceExample.class);
 
 private static List<product> products = new ArrayList<>();
 
 static {
  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/C.jgp", null));
 }
 
 
 @WebMethod(operationName="searchProduct")
 public List<Product> listAll(){
  return products;
 }
}

Step 5, Exporting Spring Bean to your web service..

It is quite easy and simple to export any Spring Bean to the Java Web Service, you just need to add Spring configuration i to wire between the Bean and Apache CXF



Step 6 , Testing your web service.

Your web service WSDL can be found at http://localhost:8080/webservicetutorial3/cxf/ProductSearchWS?wsdl

You may use SOAP UI tool to test your web service.










2 comments:

 
Blogger Templates