首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html
http://blog.csdn.net/hu_shengyang/article/details/38384597
CXF是webService技术的一种实现工具,为什么用CXF来实现webService:
1. Java的webService实现本身就是一个很耗性能的实现方案(xml与java对象之间在服务端以及客户端的互转比较消耗性能);
2. 目前java主流的webService应用以CXF、AXIS2为主;
3. 通过网络渠道的了解,目前CXF的效率要比AXIS2高出至少50%;
4. 另外有一个webService的工具metro的效率比CXF高出10%;
5. CXF的实现资料网上可以随便找出一大堆,metro的资料相对少一些;
6. CXF在java应用实现中已经很成熟,企业更倾向于用这样一个成熟的解决方案;
基于以上原因,我选择CXF来实现webService。
CXF WebService整合SpringMVC:
1、web.xml 作如下修改:
<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>/webservice/*</url-pattern> </servlet-mapping>
2、引用CXF
下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。此处用到的cxf工具包版本为:apache-cxf-2.7.13,或者maven引用:
<!-- 在properties中注明CFX的版本 --> <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> <!-- webService --> <cxf.version>2.7.13</cxf.version> </properties> <!-- webService:CXF Dependencies --> <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> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <!-- webService:End of CXF Dependencies -->
3、spring.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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="orderWebService" address="/orderWebService" implementor="com.bsit.webservice.impl.OrderWebServiceImpl" /> <bean id="client" class="com.bsit.webservice.OrderWebServiceI" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.bsit.webservice.OrderWebServiceI"/> <property name="address" value="http://192.168.1.20:8080/order/webservice/orderWebService?wsdl"/> </bean> </beans>
4、接口及实现类
定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所有方法;实现类,使用@WebService注解标注实现类,实现接口中定义的所有方法(注意@WebService(endpointInterface="接口的包名.接口名", serviceName="服务名")中的配置),如下所示:
//接口 package com.bsit.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import com.bsit.orders.model.Orders; @WebService public interface OrderWebServiceI { //使用@WebMethod注解标注WebServiceI接口中的方法 @WebMethod String sayHello(@WebParam(name="name") String name); @WebMethod Orders getOrdersByNum(String num); } //实现类 package com.bsit.webservice.impl; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import com.bsit.orders.mapper.OrdersMapper; import com.bsit.orders.model.Orders; import com.bsit.webservice.OrderWebServiceI; @WebService(endpointInterface="com.bsit.webservice.OrderWebServiceI",serviceName="OrderWebService") public class OrderWebServiceImpl implements OrderWebServiceI{ @Autowired private OrdersMapper ordersMapper; @Override public String sayHello(String name) { System.out.println("WebService sayHello " + name); return "Hello " + name + ", nice to meet you."; } @Override public Orders getOrdersByNum(String num) { Orders orders = ordersMapper.getOrdersBySalesNum(num); //数据库操作,ordersMapper注入
System.out.println(orders.getSales_order_num()); return orders; } }
5、简单效果
启动tomcat,webservice随之启动,浏览器中输入:http://192.168.1.20:8080/order/webservice,
表示webservice服务已经启动。左边是暴漏出来的接口的方法名,点击右方的WSDL的链接,可以查看方法的具体参数信息:也可以直接输入:http://192.168.1.20:8080/order/webservice/orderWebService?wsdl,打开下图
6、测试
一、
package org.bsit.webService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bsit.orders.model.Orders; import com.bsit.webservice.OrderWebServiceI; public class OrderWebService1Test { /* * 客户端调用webService的方法,如果spring.xml中配置了红色部分,则调用时factory.getBean("client"),强制转换成webService的接口 */ @Test public void testOrderWebService(){ ApplicationContext factory = new ClassPathXmlApplicationContext("/spring.xml"); //转换OrderWebServiceI OrderWebServiceI client = (OrderWebServiceI)factory.getBean("client"); Orders orders = new Orders(); orders = client.getOrdersByNum("BS20160331-2"); System.out.println(orders.getId() + ", " + orders.getSales_order_num()); } }
二、
package org.bsit.webService; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bsit.orders.model.Orders; import com.bsit.webservice.OrderWebServiceI; public class OrderWebService2Test { private OrderWebServiceI orderWebService; /* * 客户端调用webService的方法,如果spring.xml中没有配置红色部分,则调用时先写initParam()的初始化 <jaxws:endpoint id="orderWebService" address="/orderWebService" implementor="com.bsit.webservice.impl.OrderWebServiceImpl" /> */ @Before public void initParam(){ JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(OrderWebServiceI.class); factoryBean.setAddress("http://192.168.1.20:8080/order/webservice/orderWebService?wsdl"); orderWebService = (OrderWebServiceI)factoryBean.create(); } @Test public void testOrderWebService(){ Orders orders = new Orders(); orders = orderWebService.getOrdersByNum("BS20160331-2"); System.out.println(orders.getId() + ", " + orders.getSales_order_num()); } }
7、结果:
控制台打印:73ccfffc-e7a3-468e-ba7d-5dc75e67886f, BS20160331-2;
说明webService中注入mapper成功,可以进行数据库查询,简单的字符串例子没做测试。
当然,这里既可以java调用webService,也可以其他程序如.net调用。