Develop a simple WebService using the CXF framework

1. Create a new Web project as a server

#Code

Order.java

package com.bean;public class Order {private int id; private String name; private double price;public Order(int id, String name, double price) {super(); this.id = id; this.name = name; this.price = price;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Order [id=" + id + ", name=" + name + ", price=" + price + "]" ;} }

OrderWS.java

package com.ws;import javax.jws.WebMethod;import javax .jws.WebService;import com.bean.Order;@WebServicepublic interface OrderWS {@WebMethod public Order getOrderById(int id);}

OrderWSImpl.java

package com.ws;import javax.jws.WebService;import com.bean.Order;@WebServicepublic class OrderWSImpl implements OrderWS {public OrderWSImpl(){ System.out.println("OrderWSImpl()");} @ Override public Order getOrderById(int id) {// TODO Auto-generated method stub System.out.println("server getOrderById() "+id); return new Order(id, "train ticket", 20000); }}< /pre> 
#Configuration file

beans.xml

        

web.xml

< /span>

 CXFWead   index.html index.htm index.jsp< /welcome-file> default.html default.htm default.jsp    org.springframework.web.context.ContextLoaderListener    contextConfigLocation 

classpath:beans.xml CXFServlet org.apache.cxf.transport.servlet.CXFServlet 1 CXFServlet /*

2. Create a new Web project as a client

#Configure CXF environment variables, enter CMD commands to automatically generate services End code

#Configuration file

client-beans.xml

   

ClientTest.java

package com.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ws.Order;import com.ws.OrderWS;public class ClintTest {public static void main(String[] args) {// TODO Auto-generated method stub ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); OrderWS orderWS = (OrderWS) context.getBean("orderClient"); Order order = orderWS.getOrderById(24) ; System.out.println(order); }}

Screenshot:





WSDL document structure diagram

< img src="/wp-content/uploadshttp:/img.voidcn.com/vcim g/static/loading.png" alt="" d="6575332" s="56f_9b8" t="jpg">

Leave a Comment

Your email address will not be published.