cxf-jaxws-SOAP Example-Live Resturant Example
This is a simple cxf-jaxws application.The appliction offers 2 services
CXF webservice depends on the service endpoint interface.
Uses spring based configuration.
The application has been tested on tomcat and uses the latest cxf jar files.
- cancel order
- place order
CXF webservice depends on the service endpoint interface.
Uses spring based configuration.
The application has been tested on tomcat and uses the latest cxf jar files.
- Create a web application project in eclipse
- cxf.xml -add a cxf.xml in web-inf folder
- update the web.xml file --added cxf servlet and spring contextloader listener
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>cxf-jaxws-poc</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/postgreSQL-beans.xml,WEB-INF/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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
- Service EndPoint Interface-SEI
package com.live.order.service; import javax.jws.WebService; import com.live.order.domain.Order; import javax.jws.WebMethod; /** * <pre> * Service interface for Order Service operations, handles two operations. <ul> * <li>placeOrderRequest</li> * <li>cancelOrderRequest</li> * </ul> * </pre> * * * @see OrderServiceImpl * */ @WebService public interface OrderService { @WebMethod String placeOrder(Order order); @WebMethod boolean cancelOrder(String orderRef); }
-
OrderServiceImpl
package com.live.order.service; import java.io.File; import java.io.IOException; import java.util.Calendar; import javax.jws.WebService; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.ObjectUtils; import org.apache.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.live.order.dao.OrderDAO; import com.live.order.dao.OrderDAOImpl; import com.live.order.domain.Order; /** * <pre> * Service implementation for {@link OrderService} * </pre> * * @see OrderService * */ @WebService public class OrderServiceImpl implements OrderService { private static final Logger logger = Logger.getLogger(OrderServiceImpl.class); static ApplicationContext context; OrderDAO dao; public OrderServiceImpl() { context = new ClassPathXmlApplicationContext("postgreSQL-beans.xml"); dao= (OrderDAO) context.getBean("OrderDAO"); } public String placeOrder(Order order) { order.setRefNumber(getRandomOrderRefNo()); // dao.insert_Order(order); logger.info("Order has been placed. Order Info is : " + ObjectUtils.toString(order)); File file = new File("sample.txt"); System.out.println("file path : " + file.getAbsolutePath()); // We'll write the string below into the file // To write a file called the writeStringToFile // method which require you to pass the file and // the data to be written. try { FileUtils.writeStringToFile(file, ObjectUtils.toString(order)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.info("Order has been placed. Order Info is : " + getRandomOrderRefNo()); return order.getRefNumber(); } public boolean cancelOrder(String orderRef) { logger.info("Order has been cancelled Order Reference : " + orderRef); return true; } private String getRandomOrderRefNo() { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); logger.info("Ref-" + year + "-" + month + "-" + day + "-" + Math.random()); return "Ref-" + year + "-" + month + "-" + day + "-" + Math.random(); } }
- Creating domain objects
package com.live.order.domain; public class Address { protected String doorNo; protected String building; protected String street; protected String city; protected String country; protected String phoneMobile; protected String phoneLandLine; protected String email; /** * Gets the value of the doorNo property. * * @return * possible object is * {@link String } * */ public String getDoorNo() { return doorNo; } /** * Sets the value of the doorNo property. * * @param value * allowed object is * {@link String } * */ public void setDoorNo(String value) { this.doorNo = value; } /** * Gets the value of the building property. * * @return * possible object is * {@link String } * */ public String getBuilding() { return building; } /** * Sets the value of the building property. * * @param value * allowed object is * {@link String } * */ public void setBuilding(String value) { this.building = value; } /** * Gets the value of the street property. * * @return * possible object is * {@link String } * */ public String getStreet() { return street; } /** * Sets the value of the street property. * * @param value * allowed object is * {@link String } * */ public void setStreet(String value) { this.street = value; } /** * Gets the value of the city property. * * @return * possible object is * {@link String } * */ public String getCity() { return city; } /** * Sets the value of the city property. * * @param value * allowed object is * {@link String } * */ public void setCity(String value) { this.city = value; } /** * Gets the value of the country property. * * @return * possible object is * {@link String } * */ public String getCountry() { return country; } /** * Sets the value of the country property. * * @param value * allowed object is * {@link String } * */ public void setCountry(String value) { this.country = value; } /** * Gets the value of the phoneMobile property. * * @return * possible object is * {@link String } * */ public String getPhoneMobile() { return phoneMobile; } /** * Sets the value of the phoneMobile property. * * @param value * allowed object is * {@link String } * */ public void setPhoneMobile(String value) { this.phoneMobile = value; } /** * Gets the value of the phoneLandLine property. * * @return * possible object is * {@link String } * */ public String getPhoneLandLine() { return phoneLandLine; } /** * Sets the value of the phoneLandLine property. * * @param value * allowed object is * {@link String } * */ public void setPhoneLandLine(String value) { this.phoneLandLine = value; } /** * Gets the value of the email property. * * @return * possible object is * {@link String } * */ public String getEmail() { return email; } /** * Sets the value of the email property. * * @param value * allowed object is * {@link String } * */ public void setEmail(String value) { this.email = value; } @Override public String toString() { return "Address [doorNo=" + doorNo + ", building=" + building + ", street=" + street + ", city=" + city + ", country=" + country + ", phoneMobile=" + phoneMobile + ", phoneLandLine=" + phoneLandLine + ", email=" + email + "]"; } }
package com.live.order.domain; public class FoodItem { protected FoodItemType type; protected String name; protected double quantity; /** * Gets the value of the type property. * * @return * possible object is * {@link FoodItemType } * */ public FoodItemType getType() { return type; } /** * Sets the value of the type property. * * @param value * allowed object is * {@link FoodItemType } * */ public void setType(FoodItemType value) { this.type = value; } /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } /** * Gets the value of the quantity property. * */ public double getQuantity() { return quantity; } /** * Sets the value of the quantity property. * */ public void setQuantity(double value) { this.quantity = value; } }
package com.live.order.domain; public enum FoodItemType { BEVERAGES("Beverages"), COFFEE("Coffee"), DESSERTS("Desserts"), JUICES("Juices"), MEALS("Meals"), SNACKS("Snacks"), STARTERS("Starters"); private final String value; FoodItemType(String v) { value = v; } public String value() { return value; } public static FoodItemType fromValue(String v) { for (FoodItemType c: FoodItemType.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v.toString()); } }
<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:wsa="http://cxf.apache.org/ws/addressing"
xmlns:wsrm-mgr="http://cxf.apache.org/ws/rm/manager"
xmlns:wsp="http://www.w3.org/2006/07/ws-policy"
xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"
xmlns:wsam="http://www.w3.org/2007/02/addressing/metadata"
xmlns:mtom="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/schemas/configuration/wsrm-manager.xsd
http://www.w3.org/2006/07/ws-policy http://www.w3.org/2006/07/ws-policy.xsd
http://cxf.apache.org/ws/rm/manager
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="orderService" implementor="com.live.order.service.OrderServiceImpl" address="/OrderService">
</jaxws:endpoint >
</beans>
Comments
Post a Comment