Thursday, July 23, 2009

REST with CXF

REST with CXF

To understand more about rest with cxf please refer the following link
http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Introduction

In this sample code I am using apache-cxf-2.2.2 version (http://cxf.apache.org/download.html )

Let’s create a simple Restful service using cxf and tomcat. This service will be used to get the connection information.

Following files are involved in this sample application.
ConnectionInfo.java
Connection.java
ConnectionDetails.java
ConnectionInfoImpl.java
cxf.xml
web.xml

connection.java & ConnectionDetails.java :- Represent the XML JAXB Object

ConnectionInfo.java and ConnectionInfoImple : Implementation of the RestFul service

cxf.xml : cxf configuration file for REST implementation

Before getting into the details of the application, we need to setup the web application in Tomcat.

• Copy the jar files under CXF lib folder to the lib folder under Tomcat
• Add the jars under lib folder of CXF to Java Build Path/Libraries of your project.


Connection.java

package com.javatch.rest;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "Connection")
public class Connection {

protected Integer officeId;
protected String districtId;
protected String regional_id;
protected String ipAddress;
protected String queueName;
protected String instanceName;


public Integer getOfficeId() {
return officeId;
}

public void setOfficeId(Integer officeId) {
this.officeId = officeId;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

public String getDistrictId() {
return districtId;
}

public void setDistrictId(String districtId) {
this.districtId = districtId;
}

public String getRegional_id() {
return regional_id;
}

public void setRegional_id(String regional_id) {
this.regional_id = regional_id;
}

public String getQueueName() {
return queueName;
}

public void setQueueName(String queueName) {
this.queueName = queueName;
}

public String getInstanceName() {
return instanceName;
}

public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}

}

ConnectionDetails.java

package com.javatch.rest;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "ConnectionDetails")
public class ConnectionDetails {

@XmlElement(name = "connection", required = true)
List <Connection> connection;

public List<Connection> getConnection() {
if (connection == null) {
connection = new ArrayList<Connection>();
}
return this.connection;
}

}



RestFul web service Implementation

Most of the JAX-RS annotations can be inherited from either an interface or a superclass. For example :

ConnectionInfo.java

package com.javatch.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@Path("/Conn/")
public interface ConnectionInfo {

@GET
@Produces ("application/xml")
@Path("{officeId}")
public Connection getConnection(@PathParam ("officeId") int officeId);

@GET
@Produces ("application/xml")
@Path ("All")
public ConnectionDetails getAllConnections();
}



ConnectionInfoImpl.java

package com.javatch.rest;

import java.util.ArrayList;
import java.util.List;

public class ConnectionInfoImpl implements ConnectionInfo {

List <Connection> list = new ArrayList<Connection>();

ConnectionInfoImpl(){
Connection conn0 = new Connection();
conn0.setDistrictId("WA");
conn0.setInstanceName("instanceName");
conn0.setIpAddress("123.123.123.123");
conn0.setOfficeId(123);
conn0.setQueueName("Queue-Rest");
conn0.setRegional_id("WEST");
list.add(0, conn0);

Connection conn1 = new Connection();
conn1.setDistrictId("KS");
conn1.setInstanceName("instanceName");
conn1.setIpAddress("354.345.345.345");
conn1.setOfficeId(354);
conn1.setQueueName("Queue-Rest");
conn1.setRegional_id("CENTER");
list.add(1, conn1);
}

public Connection getConnection(int officeId) {
System.out.println("Inside the GetConnection...");
return list.get(0);
}

@Override
public ConnectionDetails getAllConnections() {
ConnectionDetails details = new ConnectionDetails();
for(Connection connection : list) {
details.getConnection().add(connection);
}
return details;
}

}


cxf.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:util="http://www.springframework.org/schema/util"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">

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

<jaxrs:server id="connectionService" address="/">
<jaxrs:serviceBeans>
<ref bean="connection" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>

</jaxrs:server>
<bean id="connection" class="com. javatch.rest.ConnectionInfoImpl" />
</beans>


Finally of course we need web.xml

Web.xml

<?xml version="1.0"?>
<!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>
<display-name>RestWithCXF</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/javatch/rest/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>


Now it time to test this application, deploy this under tomcat.

Use the following url’s to test this operation

http://localhost:8080/<<Root>>/services/Conn/123

or

http://localhost:8080/<<Root>>/services/Conn/All



Let me know if you have any questions 

Thanks