Sandeep is a ITA at Tata Consultancy Services. He also is the author of java blog. Sandeep has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

REST Web Service Using CXF - Beginner's Tutorial

04.26.2011
| 60364 views |
  • submit to reddit

Here we will demonstrate the creation of a sample REST web service which listens to HTTP GET requests. You can study the code to get more insight and to expand the functionality.

I will post the complete application is short time so that you can download and try the application on your own.

  1. Download CXF, Tomcat, JDK
  2. Set CATALINA_HOME to tomcat, JAVA_HOME to Jdk and CXF_HOME to cxf.
  3. Add the bin of JAVA_HOME, CATALINA_HOME and CXF_HOME to PATH variable
  4. Create a blank project named resttest
  5. Create the package/folder com.example.rest
  6. Create the following classes

    Order.java
    package com.example.rest;

    import javax.xml.bind.annotation.XmlRootElement;


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

    private int orderId;
    private String itemName;
    private int quantity;
    private String customerName;
    private String shippingAddress;

    public int getOrderId() {
    return orderId;
    }
    public void setOrderId(int orderId) {
    this.orderId = orderId;
    }
    public String getItemName() {
    return itemName;
    }
    public void setItemName(String itemName) {
    this.itemName = itemName;
    }
    public int getQuantity() {
    return quantity;
    }
    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }
    public String getCustomerName() {
    return customerName;
    }
    public void setCustomerName(String customerName) {
    this.customerName = customerName;
    }
    public String getShippingAddress() {
    return shippingAddress;
    }
    public void setShippingAddress(String shippingAddress) {
    this.shippingAddress = shippingAddress;
    }

    }
    OrderList.java
    package com.example.rest;

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

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

    @XmlElement(name = "order", required = true)
    List <Order> orders;

    public List<Order> getOrder() {
    if (orders == null) {
    orders = new ArrayList<Order>();
    }
    return this.orders;
    }
    }
    OrderInfo.java
    package com.example.rest;

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



    @Path("/Order/")
    public interface OrderInfo {

    @GET
    @Produces ("application/xml")
    @Path("{orderId}")
    public Order getOrder(@PathParam ("orderId") int officeId);

    @GET
    @Produces ("application/xml")
    @Path ("All")
    public OrderList getAllOrders();

    }
    OrderInfoImpl.java
    package com.example.rest;

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

    public class OrderInfoImpl implements OrderInfo {

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

    OrderInfoImpl(){
    Order order = new Order();
    order.setOrderId(1);
    order.setItemName("Soap");
    order.setQuantity(120);
    order.setCustomerName("Sandeep");
    order.setShippingAddress("Gurgaon");
    list.add(0, order);

    order.setOrderId(2);
    order.setItemName("Shampoo");
    order.setQuantity(50);
    order.setCustomerName("Sandeep");
    order.setShippingAddress("Gurgaon");
    list.add(1, order);
    }

    @Override
    public Order getOrder(int orderId) {
    System.out.println("Inside the GetOrder...");
    if (list.get(0).getOrderId() == orderId) {
    return list.get(0);
    } else if (list.get(1).getOrderId() == orderId) {
    return list.get(1);
    } else {
    return null;
    }
    }

    @Override
    public OrderList getAllOrders() {
    OrderList details = new OrderList();
    for(Order order : list) {
    details.getOrder().add(order);
    }
    return details;
    }
    }

    cxf.xml
    <beans xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 address="/" id="connectionService">
    <jaxrs:servicebeans>
    <ref bean="order">
    </ref></jaxrs:servicebeans>
    <jaxrs:extensionmappings>
    <entry key="xml" value="application/xml">
    </entry></jaxrs:extensionmappings>
    </jaxrs:server>
    <bean class="com.javatch.rest.OrderImpl" id="order">
    </bean></import></import></import></beans>


    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>
  7. Copy web.xml and cxf.xml into WEB-INF folder
  8. Copy the compiled classes into WEB-INF/classes folder
  9. Start the Tomcat server
  10. Browse to the location http://<ipaddress>:<port>/resttest/services/order/all
    or
    location http://<ipaddress>:<port>/resttest/services/order/

From http://extreme-java.blogspot.com/2011/04/rest-webservice-using-cxf-first.html

Published at DZone with permission of its author, Sandeep Bhandari.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Gerald Voss replied on Wed, 2011/04/27 - 2:28pm

The code is not working at all. Please include a working example. This is not a complete tutorial.

Sandeep Bhandari replied on Mon, 2011/05/02 - 8:44am in response to: Gerald Voss

You can download the war file at:

http://www.fileserve.com/file/WjT9FTD

Dawid Pytel replied on Sun, 2014/01/05 - 11:38am

Original post and source code seem to be not available. I recreated the original source code, fixed it and updated to CXF 2.7.8. Feel free to check it out: https://github.com/destin/SO-answers/tree/master/RestWithCXF

Please note that I'm not the author of this tutorial.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.