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 = 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 }
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属性此处不加也没问题-->
<!-- 如果要发布多个接口服务,再配置多个<jaxws/>即可,客户端调用时创建多个JaxWsPorxyFactoryBean对象就行了 -->
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" />
-----------------------------------------------此处非常有必要有个分割线---------------------------------------------
今天项目中需要写个对外的webservice接口,按照之前即上面的流程操作,结果悲催了,各种错误,只好再重新写了一个服务
框架 :spring mvc-4.3.7 、cxf-3.1.3、apache-maven-3.3.3
第一步、pom(!注意jar包冲突!)
必须:cxf-rt-frontend-jaxws、cxf-rt-transports-http、wsdl4j
删除: asm、XmlSchema
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bwdz.fp.saas</groupId> <artifactId>vat-web</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>vat-web</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- spring版本号 --> <spring.version>4.3.7.RELEASE</spring.version> <jjwt.version>0.6.0</jjwt.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.3</version> </dependency> <!--<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.3</version> </dependency>--> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>${spring.version}</version> </dependency> --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0-rc4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore-nio</artifactId> <version>4.4.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>com.maxmind.geoip2</groupId> <artifactId>geoip2</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.5.0</version> </dependency> <!--<dependency> <groupId>org.apache.ws.commons</groupId> <artifactId>XmlSchema</artifactId> <version>1.1</version> </dependency>--> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!--<dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.1</version> </dependency>--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.3.0.RELEASE</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.1</version> </dependency> <!-- axis 1.4 jar start --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.19</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>core-renderer</artifactId> <version>R8</version> </dependency> </dependencies> <profiles> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>product</id> <properties> <profiles.active>product</profiles.active> </properties> </profile> </profiles> <build> <finalName>vat-web</finalName> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>product/*</exclude> <exclude>dev/*</exclude> </excludes> </resource> <resource> <directory>src/main/resources/${profiles.active}/</directory> </resource> </resources> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> <configuration> <filesets> <fileset> <directory>src/main/webapp/WEB-INF/classes</directory> </fileset> </filesets> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source><!-- 源代码使用的开发版本 --> <target>1.8</target><!-- 需要生成的目标class文件的编译版本 --> <encoding>UTF-8</encoding> <optimize>true</optimize> <debug>true</debug> <showDeprecation>true</showDeprecation> <showWarnings>false</showWarnings> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <webappDirectory>src/main/webapp</webappDirectory> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
第二步:编写服务端接口和实现类:
BaseWebService接口类:
package com.bwdz.fp.saas.service.test; import javax.jws.WebService; @WebService public interface BaseWebService {
/**
* 此处有陷阱:返回类型如果是List<Map>会报错,
* 如果执意这样写,可以写个转换器,具体实现自行百度哈
**/
public String readPlcData(Integer id); }
BaseWebServiceImp实现类:
package com.bwdz.fp.saas.service.test; import com.bwdz.fp.saas.service.fphs.DzdzkZpsjTbService; import org.springframework.beans.factory.annotation.Autowired; import javax.jws.WebService; import java.util.ArrayList; import java.util.List; import java.util.Map; //@Service @WebService(endpointInterface="com.bwdz.fp.saas.service.test.BaseWebService") public class BaseWebServiceImp implements BaseWebService{ @Autowired private DzdzkZpsjTbService dzdzkZpsjTbService; @Override public String readPlcData(Integer id) { List<Map> map = new ArrayList<>(); try{ map = dzdzkZpsjTbService.getAllMcpsh(id+""); }catch (Exception e){ } return map.toString(); } }
第三步:配置spring的核心文件 application-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:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="myService"
implementor="com.bwdz.fp.saas.service.test.BaseWebServiceImp" address="/web-publish" >//address是暴漏的方法名,随便定义
</jaxws:endpoint>
第四步:配置web.xml:
<servlet> <description>CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
最后一步就是见证奇迹的时候了:
访问:http://localhost:8080/vat-gap/services/
对外只要提供一个wsdl地址就可以了:http://localhost:8080/vat-gap/services/web-publish?wsdl
客户端调用 参考我之前写的文章 https://www.cnblogs.com/maxmoore/p/9965218.html