首页 > 代码库 > spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

spring-3.1.2、cxf-3.1.3、mybaties、mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xuxiaoyu__/9868032

整合的过程其实很简单,通过简单的配置就可以,但是可悲的是 一堆问题都是由于jar缺失或者jar版本冲突引起的,所以附上全部jar文件

发布webservice服务

第一步:编写接口和实现类

PlcDataDetail.java

技术分享
 1 package test;
 2 
 3 import java.util.Date;
 4 
 5 public class PlcDataDetail{
 6 
 7     private Integer id;
 8 
 9     private Integer plc_info_address_id;
10 
11     private String value;
12 
13     private Date create_time;
14 
15     public Integer getId() {
16         return id;
17     }
18 
19     public void setId(Integer id) {
20         this.id = id;
21     }
22 
23     public Integer getPlc_info_address_id() {
24         return plc_info_address_id;
25     }
26 
27     public void setPlc_info_address_id(Integer plc_info_address_id) {
28         this.plc_info_address_id = plc_info_address_id;
29     }
30 
31     public String getValue() {
32         return value;
33     }
34 
35     public void setValue(String value) {
36         this.value = http://www.mamicode.com/value;>37     }
38 
39     public Date getCreate_time() {
40         return create_time;
41     }
42 
43     public void setCreate_time(Date create_time) {
44         this.create_time = create_time;
45     }
46 }
View Code

BaseWebService.java

 1 package xd.service.plc;
 2 
 3 import javax.jws.WebService;
 4 
 5 import xd.persistence.beans.PlcDataDetail;
 6 
 7 /**
 8  * This class was generated by Apache CXF 3.1.6
 9  * 2017-06-12T10:16:26.245+08:00
10  * Generated source version: 3.1.6
11  * 
12  */
13 //必须为targetNamespace赋值,在另一个项目调用接口时,值必须一致,不然会匹配不到
14 //(可以把发布时默认的值赋值给targetNamespace)
15 @WebService(targetNamespace = "http://impl.plc.service.xd/")
16 public interface BaseWebService {
17 
18     public PlcDataDetail readPlcData(Integer id);
19 }

BaseWebServiceImp.java

 1 package xd.service.plc.impl;
 2 
 3 import javax.jws.WebService;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 
 7 import xd.persistence.beans.PlcDataDetail;
 8 import xd.service.plc.BaseWebService;
 9 import xd.service.plc.PlcDataDetailService;
10 
11 
12 @WebService(endpointInterface="xd.service.plc.BaseWebService",serviceName="plcDataService")
13 public class BaseWebServiceImp implements BaseWebService {
14     @Autowired
15     private PlcDataDetailService plcDataDetailService;//业务处理类
16     
17     public PlcDataDetail readPlcData(Integer id) {
18         PlcDataDetail plcDataDetail = new PlcDataDetail();
19         plcDataDetail = plcDataDetailService.selectbyId(id);
20         return plcDataDetail;
21     }
22 
23 }

第二步:配置spring的核心文件 application-context.xml

添加命名空间以及配置接口发布信息:

1 xmlns:jaxws="http://cxf.apache.org/jaxws" 
2 xmlns:jaxrs="http://cxf.apache.org/jaxrs"
3 
4 xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
5            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"
6 <!-- address:暴露出来的地址,名称自己随便起 ;id属性此处不加也没问题-->
7 <jaxws:endpoint implementor="xd.service.plc.impl.BaseWebServiceImp" address="/ReadPlcData" />

配置web.xml:


<servlet-name>CXFService</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- 本系统的WebService路径必须以/ws/开头 -->
<servlet-mapping>
  <servlet-name>CXFService</servlet-name>
  <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

 访问:http://localhost:8080/ws  查看是否显示wsdl (项目通过tomcat配置不需要输入项目名,同学要注意)  结果

技术分享

 技术分享

 

 客户端调用接口:

新建一个项目,把服务的接口类BaseWebService.java拷过来,jar也要拷过来哦,编写测试类:

BaseWebService.java

 1 package test;
 2 
 3 import javax.jws.WebService;
 4 
 5 /**
 6  * This class was generated by Apache CXF 3.1.6
 7  * 2017-06-12T10:16:26.245+08:00
 8  * Generated source version: 3.1.6
 9  * 
10  */
11 @WebService(targetNamespace = "http://impl.plc.service.xd/")
12 public interface BaseWebService {
13 
14     public PlcDataDetail readPlcData(Integer id);
15 }

MyClient.java

 1 package test;
 2 
 3 import java.util.Properties;
 4 
 5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 6 
 7 public class MyClient {
 8     public static void main(String args[]) { 
 9         //下面三行代码是系统初始化设置,若独立于服务接口外调用,不加也可以,如果在接口项目内调用需要加上,不然会报错
10         Properties props = System.getProperties(); 
11         props.setProperty("org.apache.cxf.stax.allowInsecureParser", "1"); 
12         props.setProperty("UseSunHttpHandler", "true");
13         //创建WebService客户端代理工厂
14         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
15         //注册WebService接口   
16         factory.setServiceClass(BaseWebService.class);
17         //设置WebService地址
18         factory.setAddress("http://localhost:8080/ws/ReadPlcData");
19         BaseWebService service = (BaseWebService) factory.create();
20         System.out.println(service.readPlcData(14).getValue());//传参查询
21     }
22 }

 

  总结:网上的资源很多,有新资源,也有很久之前的了,在整合的时候出现了很多冲突,要注意自己项目中cxf的版本。比如刚开始顺着网上说的 在spring文件中配置下面三行资源,结果总是报错说找不到,其实在cxf 3.x 版本后就不需要再手动配置了,所以找问题要找最新的,老的不一定适用,用旧版本在开发过程中可能就是个大坑啊。

<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" />

 

spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)