Home > Web Services > Make your Web Services simple with Apache CXF and Spring

Make your Web Services simple with Apache CXF and Spring

Have you ever just wanted to make Java Web Services so easy that you can whip one out in a few minutes, well here is your shot. Apache CXF and Spring make creating a Web Service almost stupid simple. I’ll get right to it.

First you need to configure CXF in your web application by adding a few lines to the web.xml.

CXFServlet

org.apache.cxf.transport.servlet.CXFServlet

CXFServlet
/ws/*

All this is doing is setting it up so that all requests for /ws/* to get routed to CXF. Now we need to include a new xml file for Spring to load.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:cxf=”http://cxf.apache.org/core”
xmlns:jaxws=”http://cxf.apache.org/jaxws”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd”
default-autowire=”byName”>


<bean id="aegisBean"
class=”org.apache.cxf.aegis.databinding.AegisDatabinding”
scope=”prototype”>

<bean id="jaxws-and-aegis-service-factory"
class=”org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean”
scope=”prototype”>

<jaxws:endpoint id="applicationWebService"
implementorClass=”com.domain.services.impl.ApplicationServiceImpl”
implementor=”#applicationService”
address=”/applicationService”>

Now, we are ready to write our service class. I typically create service classes to interact with my other backend services, databases, web services, etc. Here is an example.

@WebService(endpointInterface = “com.domain.services.IApplicationService”, name = “applicationService”, serviceName = “applicationService”, portName = “applicationPort”, targetNamespace = “http://services.domain.com/”)
@Service(name=”applicationService”)
public class ApplicationServiceImpl extends ServiceImpl implements
IApplicationService {

public String sayHello(){

return “Hello”;
}

And that is pretty much it. Start up your Web server and hit /ws/applicationService?wsdl and you should get a WSDL for the service automatically generated. You can use SoapUI as a testing tool to test out the services. You can also use SoapUI to generate any type of client that you want for the services, JAX-WS, JAX-RPC, Axis, you name it.

It doesn’t get much easier than that does it? I’ve only scratched the surface of CXF as well. It has many more features.

Spring and CXF make a truly versatile way to create an SOA architecture or an Enterprise Service Bus all on your own as well.

Advertisement
Categories: Web Services
  1. Anonymous
    May 1, 2010 at 12:15 am | #1

    now lets see a client

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s