Apache CXF Web Services Tutorial

I created a web service today with CXF and wanted to share the steps it took to get it up and running in this quick tutorial. Apache CXF was created by the merger of the Celtix and XFire projects. , the documentation is still a work in progress. However, do not fret because this CXF tutorial will get you up and running in no time. I will be creating a simple web service that will allow the retrieval of employee information. The service will return this simple POJO (Plain Old Java Object) bean with matching getters and setters:

package com.company.auth.bean;

import java.io.Serializable;
import java.util.Set;

public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
private String gid;
private String lastName;
private String firstName;
private Set privileges;

public Employee() {}

public Set getPrivileges() {
return privileges;

public void setPrivileges(Set privileges) {
this.privileges = privileges;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getGid() {
return gid;

public void setGid(String gid) {
this.gid = gid;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

public boolean isUserInRole(String role) {
if(privileges == null) { return false; }
else { return privileges.contains(role); }


First off, you need to download Apache CXF and drop the necessary .jars in your WEB-INF/lib directory:

geronimo-activation_1.1_spec-1.0-M1.jar (or Sun’s Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun’s JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun’s Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)


The first thing which needed to be done was to create the service interface. The service interface defines which methods the web service client will be able to call. It’s pretty standard Java with just two JWS (Java Web Service) annotations thrown in:

package com.company.auth.service;

import javax.jws.WebService;
import javax.jws.WebParam;
import com.company.auth.bean.Employee;

public interface AuthService {
Employee getEmployee(@WebParam(name="gid") String gid);

Implementing the actual service comes next:

package com.company.auth.service; import javax.jws.WebService; import com.company.auth.bean.Employee; import com.company.auth.dao.EmployeeDAO; @WebService(endpointInterface = "com.company.auth.service.AuthService", serviceName = "corporateAuthService") public class AuthServiceImpl implements AuthService { public Employee getEmployee(String gid) { EmployeeDAO dao = new EmployeeDAO(); return dao.getEmployee(gid); } }

I then had to tell Spring (which is used by CXF) where to find my Java classes. I created the following cxf.xml file inside my package directory (com/company/auth/service):

Finally, I updated my WEB-INF/web.xml file to let CXF know where my cxf.xml file was and define the CXF servlet:

Auth Manager contextConfigLocation classpath:com/company/auth/service/cxf.xml org.springframework.web.context.ContextLoaderListener CXFServlet org.apache.cxf.transport.servlet.CXFServlet CXFServlet /services/*

The most frustrating portion of getting the CXF web service up and running was the following exception:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.jws.WebService.portName()Ljava/lang/String;

This is due to the fact that I am running WebLogic 9.2, which contains a library with an older version of JSR (Java Specification Request) 181. The quickest solution for me was to prepend geronimo-ws-metadata_2.0_spec-1.1.1.jar to the WebLogic classpath. This will likely not be the solution I choose to implement in the end, but it got me back up and running. It seemed I also had to clear my WebLogic cache for this fix to take effect. Because I often find instances where this seems necessary, I have created a Windows script to clear the Weblogic Cache. If you run into app server related issues, this was one area I found the Apache CXF docs to be helpful.

Also, if you are running Hibernate, you may encounter ASM incompatibilities between Hibernate’s CGLib and the version of ASM which CXF requires. But do not fret because this is easy enough to solve.

Once this was solved, the list of services was available. The context root for my web project is authManager, so my regular web index page is available at http://localhost:7001/authManager/. The list of web services is then automagically generated by CXF at http://localhost:7001/authManager/services/.

The final step is to build the client. This was very easy when compared to getting the server up and running because I was able to simply swipe the code from the Apache CXF documentation. So, without further ado:

package com.company.auth.client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.company.auth.bean.Employee; import com.company.auth.service.AuthService; public final class Client { private Client() { } public static void main(String args[]) throws Exception { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(AuthService.class); factory.setAddress("http://localhost:7001/authManager/services/swAuth"); AuthService client = (AuthService) factory.create(); Employee employee = client.getEmployee("0223938"); System.out.println("Server said: " + employee.getLastName() + ", " + employee.getFirstName()); System.exit(0); } }

Wow! Wasn’t that cool? (Yes, I’m a dork :o) You should now be up and running with a CXF web service.

This article is taken from : http://www.benmccann.com/dev-blog/web-services-tutorial-with-apache-cxf/


The @WebParam annotation is in fact optional, but highly recommended since it will make like easier for the end consumers of your service. Without it, your parameter would be named arg0 making it less clear what parameters your service actually takes.



Popular posts from this blog

PostgreSQL bytea and oid

MySQL as Hive metadata store

Big Memory Go.