A simple summary of CXF and rest with the practice of webservice, directly to the code example:
1 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;
}
}
2 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;
}
}
3 Orderinof.java Interface
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 ();
}
4 Orderinfoimpl.java Interface Implementation class
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;
}
}
Configuration of the CXF
<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>
Configuration of Web. XML Add CXF Configuration
<?
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>
5 Final execution
Htttp://localhost:8085/reset/services/order/all
Return to all order list
Create a rest WEBSERVICE using CXF