使用CXF发布WebService简单又快速,还可以与Spring集成,当Web容器启动时一起发布WebService服务。本例是简单的客户端给服务端发送订单信息,服务端返回订单转为json的字符串。
1.使用maven管理jar包,首先在maven添加使用到的cxf jar包依赖,到CXF官网上找到Maven的依赖内容。
网址如下:http://cxf.apache.org/docs/using-cxf-with-maven.html
我使用的是Tomcat所以引用前两项就可以了
<properties> <cxf.version>2.2.3</cxf.version> </properties>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency>
其它jar包依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency>
2.创建简单的订单类:
package com.ken.model; import java.io.Serializable; public class Order implements Serializable { private String OrderNo; private String itemCode; private String itemName; private Long quantity; public String getOrderNo() { return OrderNo; } public void setOrderNo(String orderNo) { OrderNo = orderNo; } public String getItemCode() { return itemCode; } public void setItemCode(String itemCode) { this.itemCode = itemCode; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public Long getQuantity() { return quantity; } public void setQuantity(Long quantity) { this.quantity = quantity; } }
3.WebService接口及实现类:
3.1接口
package com.ken.webservice; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import com.ken.model.Order; @WebService public interface IRemoteService { public String showRemoteMessage(@WebParam(name="arg0") String message); public String addOrder(List<Order> orders); }
3.2接口实现类
package com.ken.webservice.impl; import java.util.List; import javax.jws.WebService; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.ken.model.Order; import com.ken.webservice.IRemoteService; @WebService(endpointInterface="com.ken.webservice.IRemoteService") //endpointInterface这个不能写错了 public class RemoteServiceImpl implements IRemoteService { public String showRemoteMessage(String message) { return "You message is "+ message; } public String addOrder(List<Order> orders) { return JSONArray.toJSONString(orders,true); } }
4.配置Spring-context.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath*:META-INF/cxf/cxf.xml" /> <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="remoteServiceEndPoint" implementor="com.ken.webservice.impl.RemoteServiceImpl" address="/RemoteService"></jaxws:endpoint> </beans>
5.配置web.xml文件
<!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>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.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>/services/*</url-pattern> </servlet-mapping> </web-app>
完成上面的步骤后就可以启动Tomcat并访问
wsdl:
http://localhost:8080/IHealth.Web/services/RemoteService?wsdl
6.客户端调用服务
cmd到其项目的src目录并执行命令就会解析生成相应的调用代码,命令:wsimport -keep http://localhost:8080/IHealth.Web/services/RemoteService?wsdl
生成好刷新下src目录就会看到生成的代码,如下
此时就可以写调用代码了:
package com.ken.client; import java.util.ArrayList; import java.util.List; import com.ken.webservice.Order; import com.ken.webservice.impl.IRemoteService; import com.ken.webservice.impl.RemoteServiceImplService; public class Client { public static void main(String[] args) { //创建一个用于产生RemoteServiceImplService实例的工厂,RemoteServiceImplService类是wsimport工具生成的 RemoteServiceImplService factory = new RemoteServiceImplService(); //通过工厂生成一个IRemoteService实例,IRemoteService是wsimport工具生成的 IRemoteService service = factory.getRemoteServiceImplPort(); String result = service.showRemoteMessage("Ken"); System.out.println(result); List<Order> orders = new ArrayList<Order>(); Order order1 = new Order(); order1.setOrderNo("201706048888"); order1.setItemCode("A10000001"); order1.setItemName("IPHONE"); order1.setQuantity(100L); orders.add(order1); Order order2 = new Order(); order2.setOrderNo("201706048888"); order2.setItemCode("A10000002"); order2.setItemName("MAC"); order2.setQuantity(200L); orders.add(order2); String resultOrder = service.addOrder(orders); System.out.println(resultOrder); } }
运行后控制台内容:
You message is Ken [ { "itemCode":"A10000001", "itemName":"IPHONE", "orderNo":"201706048888", "quantity":100 }, { "itemCode":"A10000002", "itemName":"MAC", "orderNo":"201706048888", "quantity":200 } ]