Developing RESTful Services using Apache CXF

Introduction

As you already know there are two ways of developing a web service

  1. Simple Object Access Protocol (SOAP)
  2. Representational State Transfer (REST)

Before jumping on how to create a REST based web service using Apache CXF we shall see what is REST. REST is not a technology and certainly is not a standard of some kind. It is merely an architectural style that chalks down how to write a web service in a certain way. This style was defined by a certain Roy Fielding (ring bells? yep you guessed right, he is one of the architects of HTTP) in 2000. Principal protagonist of a REST architecture is a Resource which can be uniquely identified by an Uniform Resource Identifier or URI. State of a resource at any given point of time is represented by a document and is called Representation of resource. The client can update the state of resource by transferring the representation along with the request. The new representation is now returned to client along with the response.  The representation contains the information in formats like html, xml, JSON etc that is accepted by the resource. The resource which adheres to rules of REST architecture is called a RESTfull resource and web service that adheres to this rule are called RESTfull web service.

Create a project to contain your web service

I generally do my web development in struts2+spring using maven Strut2 starter archetype to create my web project. To use CXF in my project I add following dependencies to my POM

<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>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http-jetty</artifactId>
	<version>${cxf.version}</version>
</dependency>

The non maven users can find the details of dependencies to be added in following link

http://cxf.apache.org/docs/using-cxf-with-maven.html

The CXF can be directly downloaded from here

http://cxf.apache.org/download.html

How to Create a CXF RESTfull web-service?

Suppose you want to create a RESTfull web service using CXF for managing books in your personal bookshelf.  You would typically want to perform following operations on the bookshelf

  1. Add a book
  2. Update book information
  3. Delete a book from the shelf
  4. Get a book
  5. Get book list
  6. Get book list by author name

Following steps are needed create such a service

  1. Create BookVO, BookList (value object) for passing as representation in request and response.
  2. Bind the objects with request and response.
  3. Create the service implementation class to accept request and generate response.
  4. Registering your webservice with CXF container.
  5. Deploy the service in a web container.
  6. Create clients to invoke methods on the service.

Getting the source code for this tutorial

I have committed the source files for this tutorial in SVN.

You can download the web app from

http://subversion.assembla.com/svn/weblog4j/Weblog4jDemo/trunk

You can download the client from

http://subversion.assembla.com/svn/weblog4j/DemoClient/trunk

Note: Both are ItelliJ maven projects so you can directly import them to your intelliJ IDE or copy over the files manually to other IDE


Create BookVO (value object) for passing as representation in request and response.

BookVO Class

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.Serializable;

@XmlRootElement(name="Book")
public class BookVO implements Serializable{

private long bookId;
private String bookName;
private String author;

public long getBookId() {
return bookId;
}

public void setBookId(long bucketId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}

BookList Class

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name="BookList")
public class BookList {
private List<BookVO> bookList;

public List<BookVO> getBookList() {
if(bookList == null){
bookList = new ArrayList<BookVO>();
}
return bookList;
}

public void setBookList(List<BookVO> bookList) {
this.bookList = bookList;
}
}

Bind the Data object i.e. BookVO with request and response

To bind the BookVO with the request or response it needs to serialized into either XML or JSON streams. The serialization needs to be done using one of the data binding components. CXF uses JAXB for default data binding component. JaXB uses @XmlRootElement annotation to map the data object to the xml. You can see the use of XmlRootElement annotation in code above.

Create the service implementation class to accept request and generate response

Let us see how a CXF RestFull webservice looks. We will create a BookService class which will perform add,update,delete and get operations on BookSelf.

BookService Class

package com.aranin.weblog4j.services.rest;


import com.aranin.weblog4j.hashdb.HashDB;
import com.aranin.weblog4j.vo.BookVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 3/13/13
 * Time: 3:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class BookService {

protected final Logger log = LoggerFactory.getLogger(BookService.class);


    @POST
    @Path("/getbook/{name}")
    @Produces({"application/xml","application/json"})
    @Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
    public Response getBucket(@PathParam("name") String name) {
        log.debug("name : " + name);
        BookVO bookVO = null;
        try {
            bookVO = HashDB.getBook(URLDecoder.decode(name, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        if(bookVO == null){
            return Response.status(Response.Status.BAD_REQUEST).build();
        }else{
            return Response.ok(bookVO).build();
        }
    }

    @POST
    @Path("/addbook")
    @Produces({"application/xml","application/json"})
    @Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
    public Response addBook(@FormParam("name") String bookName,
                            @FormParam("author") String author) {
        log.debug("inside addBook");
        BookVO bookVO = new BookVO();
        bookVO.setBookName(bookName);
        bookVO.setAuthor(author);
        HashDB.insertBook(bookVO);
        if(HashDB.getBook(bookName) == null){
            return Response.status(Response.Status.BAD_REQUEST).build();
        }else{
            return Response.ok(bookVO).build();
        }

    }
}

You can see two methods in the BookService class getBook and addBook. They are service methods for getting and adding a book. Rest of the methods for update delete etc can be written in same way. Now lets see what the various annotations and method call means.

@POST – This indicates that service receives only POST request.

@Path – This is the path of webservice. So the webservice can be invoked using following Url <base_url>/bookservice/getbook/{name} for fetching, <base_url>/bookservice/addbook for adding.

@Produces – Indicates the MIME type of response generated. In our case it is both application/xml and application/json.

@Consumes – Indicates the MIME type of request which this service can consume.

Registering your webservice with CXF container.

One cool thing with CXF is that it uses a spring based configuration for registering its webservice endpoints so let us create a beans.xml in WEB-INF and configure the CXF in web.xml. For this first we need to wire the beans.xml to get loaded by spring container.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>

Secondly load the register the CXFServlet in web.xml.

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Now open your bean.xml and register your bookservice end point.

<?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:jaxws="http://cxf.apache.org/jaxws"
    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://cxf.apache.org/jaxrs


http://cxf.apache.org/schemas/jaxrs.xsd


http://cxf.apache.org/jaxws


http://cxf.apache.org/schemas/jaxws.xsd">

	<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="bookShelfService"
	  implementor="com.aranin.weblog4j.services.BookShelfServiceImpl"
	  address="/bookshelfservice" />


    <bean id="bookserviceclass" class="com.aranin.weblog4j.services.rest.BookService"/>


    <jaxrs:server id="bookservice" address="/bookservice">
        <jaxrs:serviceBeans>
        <ref bean="bookserviceclass" />
        </jaxrs:serviceBeans>
    </jaxrs:server>



</beans>

Now your webservice is ready. Build your web application and deploy it in any servlet container.

Creating Client for your webservice

Clients can be created in many ways, I have used apache Http Components to write my client. The libraries can be found in http://hc.apache.org/httpclient-3.x/.

Maven user can pull the Http components jar using following

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.3</version>
<scope>compile</scope>
</dependency>

Now to invoke the webservice I have created a util class called DemoRestClient.

package com.aranin.weblog4j.client;


import com.aranin.weblog4j.vo.BookVO;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.net.URLEncoder;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 3/13/13
 * Time: 4:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class DemoRestClient {
    public static void main(String[] args){
        DemoRestClient restClient = new DemoRestClient();
        try {
            //restClient.addBook("Naked Sun", "Issac Asimov");
            restClient.getBook("Naked Sun");
        } catch (Exception e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public BookVO getBook(String bookName) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/weblog4jdemo/bookservice/getbook/";

            url = url + URLEncoder.encode(bookName, "UTF-8");

            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            client.executeMethod( mPost );
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("out : " + output);
        }catch(Exception e){
            throw new Exception("Exception in retriving group page info : " + e);
        }
        return null;
    }

    public void addBook(String bookName, String author) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/weblog4jdemo/bookservice/addbook";
            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            mPost.addParameter("name", "Naked Sun");
            mPost.addParameter("author", "Issac Asimov");
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            //mtHeader.setValue("application/json");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("output : " + output);
        }catch(Exception e){
        throw new Exception("Exception in adding bucket : " + e);
        }

    }

}

Run this client to see the output of your webservice. Right now it will sending xmloutput as the response accept header is “application/xml”. You can change it to application/json to get an json output.

Thats all folks. This is a very basic introduction to developing RestFull web service using apache CXF, there is a lot more to explore. Happy exploring, till then good bye. Please drop some comments as and if you read this to keep me inspired :)

 

Print Friendly

About Niraj Singh

I am CEO and CoFounder of a startup "Aranin Software Private Limited, Bangalore. I completed my graduation in 2002 as an Aerospace Engineer from IIT Kharagpur. I love working on new ideas and projects and recently released my first open source project JaiomServer "http://jaiomserver.org". I have 9 years of experience in IT industries most of which I have spent in developing community applications for various clients using java. Some of the sites in which I have actively involved with are hgtv.com, food.com, foodnetwork.com, pickle.com, diynetwork.com etc.
This entry was posted in Apache CXF, REST, web service and tagged , , , . Bookmark the permalink.

8 Responses to Developing RESTful Services using Apache CXF

  1. Pingback: Developing SOAP Web service using Apache CXF | Java Logs

  2. The parameter “com.sun.jersey.config.property.package” defines in which package jersey will look for the web service classes. This property must point to your resources classes. The URL pattern defines part of the base URL your application will be placed.

  3. Hi All, I am writing a RESTful web service which will take a XML file as input and process the same, and save it into database. I am new to RESTful web services(jersey), can anybody please help me how to achieve the things using jersey. 1- Please give any link if you have? 2- Sample code which will help me to undersatnd how I ll post xml to jersey. 3- May be any API where I can learn and do. 4- If we cant not achieve that or not good to do that way please suggest what is the best way to send XML file to my app using webservice. 5- Keep in mind I need a secure web service, I may send user credential with the xml file as a header/element. Waiting for quick response as it is little urgent for me. Many thanks in advance for your help.

  4. Lolly says:

    You’ve got a great blog here well done congratulations

  5. Developing RESTful Services using Apache CXF | Java Logs

  6. Thanks for the new things you have discovered in your writing. One thing I’d prefer to comment on is that FSBO connections are built over time. By launching yourself to the owners the first saturday and sunday their FSBO is usually announced, prior to a masses get started calling on Mon, you generate a good association. By giving them tools, educational products, free reviews, and forms, you become a great ally. By using a personal affinity for them as well as their predicament, you make a solid relationship that, on most occasions, pays off when the owners opt with a representative they know and also trust preferably you actually.

  7. here on the internet resource assembled a huge assortment of news about новости интернета 2013.