Suggested Pages

Tuesday, September 11, 2012

Web Service with Apache CXF - Tutorial


In this post we'll see an example of a Web Service with Apache CXF using Maven.

Step 1: Declare Maven dependencies in your pom.xml


pom.xml

 <!-- apache cxf  --> 
 <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-frontend-jaxws</artifactId>
     <version>${cxf.version}</version>
 </dependency>
 <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
 </dependency>

 <!-- spring --> 
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context-support</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
 </dependency>


Step 2: Add CXFServlet into the web.xml of your Web application



<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>apachecxfwsexample1</display-name>


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

 <!-- middleware configuration -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:META-INF/spring/spring-*.xml</param-value>
 </context-param>

 <!-- Apache CXF Servlet -->
 <servlet>
     <servlet-name>CXFServlet</servlet-name>
     <servlet-class>
         org.apache.cxf.transport.servlet.CXFServlet
     </servlet-class>
   </servlet>
 
 <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>


Step 3: Create the Web Service


First write the SEI (Service Endpoint Interface) HelloService annotated with @WebService, then the class HelloServiceImpl that implements the interface.

HelloService.java
package com.simonefolinoblogspot.apachecxf.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * 
 * 
 * Service Endpoint Interface (SEI)
 *
 */
@WebService
public interface HelloService {
 
 @WebMethod(operationName="sayHello")
 public String sayHello(@WebParam(name="name")String name);

}


HelloServiceImpl.java

package com.simonefolinoblogspot.apachecxf.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService(endpointInterface="com.simonefolinoblogspot.apachecxf.ws.HelloService",serviceName = "HelloService")
public class HelloServiceImpl implements HelloService{

 
 @WebMethod(operationName="sayHello")
 public String sayHello(@WebParam(name="name")String name) {
  return "hi "+name+" !";
 }

}

Step 4: Create the configuration file cxf-servlet.xml


In the file web.xml it's declared cxf-servlet.xml as the file to put the configurations of web services.
cxf-servlet.xml


<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">
 
 <!-- START needed to import apache cxf configuration START -->
 <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" />
 <!-- END needed to import apache cxf configuration  END-->

 <!-- the service is available at http://<hostname>/<context>/helloservice -->

 <jaxws:endpoint id="helloservice"
  implementor="com.simonefolinoblogspot.apachecxf.ws.HelloServiceImpl"
  address="/helloservice" />
</beans>


Step 5: Write a Client to test the web service




package com.simonefolinoblogspot.apachecxf.ws.client;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import com.simonefolinoblogspot.apachecxf.ws.HelloService;

public final class Client {

    private static final QName SERVICE_NAME  = new QName("http://ws.apachecxf.simonefolinoblogspot.com/", "HelloService");
    private static final QName PORT_NAME = new QName("http://ws.apachecxf.simonefolinoblogspot.com/", "HelloServicePort");


    private Client() {
    } 

    public static void main(String args[]) throws Exception {
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:8083/apachecxfwsexample1/helloservice";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        
        HelloService hw = service.getPort(HelloService.class);
        System.out.println(hw.sayHello("Simone"));

    }

}

No comments :

Post a Comment

Suggested Pages