Over a million developers have joined DZone.

REST Web Service Using CXF - Beginner's Tutorial

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

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

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Topics:
java,cxf,tomcat

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}