Archive

Posts Tagged ‘CXF’

Tutorial – Your first CXF web service in 10 minutes

December 16th, 2009 Karthikeyan C 1 comment

This post is for Java developers who wish to start developing web services using CXF.It is assumed you have Tomcat 6.0.x installed on your machine and an IDE that supports Maven projects.

There are 2 Maven projects involved.

  • Project 1 is the web application (deployed in Tomcat) which contains the CXF web service which accepts time in milliseconds and returns the related java.util.Date object.
  • Project 2 is the client side standalone Java code invoking this web service.

Please click here to browse/checkout the source code of the web application is available in Google code.

Please click here to browse/checkout the source code of the standalone Java client.

We start with the web service interface provided below which has the method to convert time in milli seconds to Date format.


package com.karthik;

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

@WebService
public interface DateConversionService {
 java.util.Date convertDate(@WebParam(name="timeInMilliSecs") Long timeInMilliSecs);
}

The implementation class is as below.


package com.karthik;

import java.util.Date;
import javax.jws.WebService;

@WebService(endpointInterface="com.karthik.DateConversionService")
public class DateConversionServiceImpl implements DateConversionService{

 @Override
 public Date convertDate(Long timeInMilliSecs) {
 return new Date(timeInMilliSecs);
 }

}

We have to inform CXF to deploy the web service by providing the necessary details in a Spring Bean configuration file. The file name is cxf.xml and contains the following code (Few XML name spaces are not needed but provided so that we can reuse the same for future posts in CXF series).


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

 <!-- Load CXF modules from cxf.jar -->
 <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="dateconversion"  address="/DateConversionWS" implementor="com.karthik.DateConversionServiceImpl"/>

</beans>

Now in web.xml we configure the URL at which CXF servlet will be listening and also inform the location of the configuration file (that is the above cxf.xml).


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <displayname>karthikeyanc.com CXF example</displayname>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:cxf.xml</param-value>
 </context-param>
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 <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>/services/*</url-pattern>
 </servlet-mapping>
 </web-app>

Note: The pom.xml of this web application uses Maven assembly plugin to pack the web service interface which will  be referred as a dependency from the standalone Java client. By doing this we do not duplicate the web service interface file.

Please navigate to the project root folder and run mvn install. Under Tomcat_Home/conf/Catalina/localhost, create file called convertdate.xml to configure the context for this web application (As convertdate) and point to the target directory of the maven project as below. (Please change the docBase accordingly). Then start Tomcat. The WSDL can be obtained by typing the following in the URL http://localhost:8080/convertdate/services/DateConversionWS?wsdl


<Context path="/convertdate" docBase="/home/pathtoprojectroot/cxfwebappdateconverter/target/cxfdate"/>

Running the standalone Java client:

Now you may open the stand alone client project in an IDE (I use NetBeans 6.8) and run DateConversionServiceClient.java. This is the only Java file in the client project. The web service interface comes from the distribution jar ( created when running mvn install command for the web application Maven project). The source code of DateConversionServiceClient.java is as below. (Please change port number accordingly).


package com.karthik.client;

import com.karthik.DateConversionService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DateConversionServiceClient {

 private static final String ADDRESS="http://localhost:8080/convertdate/services/DateConversionWS";
 public static void main(String[] args) {
 JaxWsProxyFactoryBean fb=new JaxWsProxyFactoryBean();
 fb.setAddress(ADDRESS);
 fb.setServiceClass(DateConversionService.class);
 DateConversionService service=(DateConversionService) fb.create();
 System.out.println("The date converted is :"+service.convertDate(System.currentTimeMillis()));

 }
}
  • Share/Bookmark
Categories: Development Tags: ,