Saturday 27 April 2013

Spring web-service using Apache CXF

What is Apache CXF?



CXF is an open source fully featured Web services framework provided by Apache. CXF origination is a combination of two open source projects named Celtix(acquired by Progress Software) and Xfire(developed by Codehaus).


  • It helps us to build and develop services using frontend programming APIs, like JAX-WS and JAX-RS.

  • These services can talk with a variety of protocols like SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI (Java Business Integration).

Note: - Java Business Integration (JBI) is a specification developed under the Java Community Process (JCP) for an approach to implement a SOA (service-oriented architecture).  It is built on a Web Services model and provides a pluggable architecture for a container that hosts service producer and consumer components.

spring web service using Apache CXF Sample project development :-


Steps for Code development:-
  • Download CXF related jars from below location: - 
         http://www.apache.org/dyn/closer.cgi?path=/cxf/2.5.10/apache-cxf-2.5.10.zip

  •  Create a dynamic web project using Eclipse and named it as SpringWSUsingCXF.
  •  Create a package name with com.spring.cxf.webservice in the src folder.
  • Create another package in the same folder and name it as com.spring.cxf.webservice.client.
  • Place InternetAddress.java, InternetAddressProcess.java and InternetAddressProcessImpl.java file in the above created package named com.spring.cxf.webservice.
         (Below is the source code available for InternetAddress.java, InternetAddressProcess.java and 
         InternetAddressProcessImpl.java).

  • Place the java file CXFWebServiceClient.java in the package com.spring.cxf.webservice.client.
         (Below is the source code available for CXFWebServiceClient.java).

  • Create an XML file in the package com.spring.cxf.webservice.client and name it as client-webservice-beans.xml.
  • Create web.xml file in WEB-INF directory and place the below source code available in that file.
  • Create beans.xml file in WEB-INF directory and place the below source code available in that file.
  • Deploy the project on Server for that refer STEP-15 from the below link. http://www.javatechtipssharedbygaurav.com/2013/04/rest-web-service-with-crud-operations.html





InternetAddress.java

package com.spring.cxf.webservice;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class InternetAddress {
    public InternetAddress() {
    }

    public String getSystemDetails() {
        InetAddress ip;
        StringBuilder strBuild = new StringBuilder();
        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get hostname
            String hostName = addr.getHostName();

            // Get hostAddress
            String ipAddress = addr.getHostAddress();

            ip = InetAddress.getLocalHost();

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i],
                        (i < mac.length - 1) ? "-" : ""));
            }
            strBuild.append("hostName:-").append(hostName)
                    .append(" , hostAddress:-").append(ipAddress)
                    .append(" , macAddress:-").append(sb.toString());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return strBuild.toString();
    }
}

InternetAddressProcess.java

package com.spring.cxf.webservice;

import javax.jws.WebService;

@WebService
public interface InternetAddressProcess {
    String processUserIPAddress(InternetAddress internetAdd);
}

InternetAddressProcessImpl.java

package com.spring.cxf.webservice;

import javax.jws.WebService;

@WebService(endpointInterface = "com.spring.cxf.webservice.InternetAddressProcess")
public class InternetAddressProcessImpl implements InternetAddressProcess {

    public String processUserIPAddress(InternetAddress internetAdd) {
        return internetAdd.getSystemDetails();
    }
}

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxws:endpoint
      id="internetAddressProcess"
      implementor="com.spring.cxf.webservice.InternetAddressProcessImpl"
      address="/InternetAddressProcess" />
     
</beans>

CXFWebServiceClient.java

package com.spring.cxf.webservice.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.cxf.webservice.InternetAddress;
import com.spring.cxf.webservice.InternetAddressProcess;

public final class CXFWebServiceClient {

    public CXFWebServiceClient() {
    }

    public static void main(String args[]) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "com/spring/cxf/webservice/client/client-webservice-beans.xml");

        InternetAddressProcess client = (InternetAddressProcess) context
                .getBean("client");
        InternetAddress internetAdd = new InternetAddress();

        String ipDetails = client.processUserIPAddress(internetAdd);
        System.out.println("System Address Details in Client:-" + ipDetails);

    }
}

client-webservice-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <bean id="client" class="com.spring.cxf.webservice.InternetAddressProcess"
      factory-bean="internetFactory" factory-method="create"/>
      <bean id="internetFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name="serviceClass" value="com.spring.cxf.webservice.InternetAddressProcess"/>
      <property name="address" value="http://localhost:7075/SpringWSUsingCXF/InternetAddressProcess"/>
    </bean>
</beans>

Project Structure is as below:-



The Project Library files which I am using with this project is shown below:-

 

Result:-

When we deploy this project in the server, then in this URL:- http://localhost:7075/SpringWSUsingCXF/
It will show the below result:-

In the Link shown in the above image, we will get out WSDL file, The URL is as below:-
http://localhost:7075/SpringWSUsingCXF/InternetAddressProcess?wsdl

Now the time to execute the client program, we can execute the CXFWebServiceClient as Run as Java application and after execution we will get below result:-

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Apr 27, 2013 7:25:05 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://webservice.cxf.spring.com/}InternetAddressProcessService from class com.spring.cxf.webservice.InternetAddressProcess
 

System Address Details in Client:-hostName:-home-3214569870 , hostAddress:-123.112.98.221 , macAddress:-11-2F-67-11-C1-CB


Note:- I have taken all the jars available in the CXF lib directory and also added spring related jars as we can see in the project library figure.




No comments:

Post a Comment