1、Maven包依赖(此步非常重要,包不全会出现许多类型报错)
<properties>
<cxf.version>3.1.11</cxf.version>
</properties>
<dependencies>
<!-- 如果改为此servlet-api,则会出现webservice发布成功,但wsdl无法访问情况 -->
<!--<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.32</version>
</dependency>-->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.20100224</version>
</dependency>
<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-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
2、发布服务
/**
* @author : Jian Shen
* @version : V1.0
* @date : 2018/10/20
*/
@WebService
public class WorkFlowService {
@WebMethod
public void execute() {
System.out.println("执行流程中心");
}
public static void main(String[] args) {
JaxWsServerFactoryBean serverFactoryBean = new JaxWsServerFactoryBean();
serverFactoryBean.setAddress("http://127.0.0.1:9082/cxf/service/workflow");
serverFactoryBean.setServiceClass(WorkFlowService.class);
serverFactoryBean.setServiceBean(new WorkFlowService());
Server server = serverFactoryBean.create();
if (server.isStarted()) {
System.out.println("webservice 发布成功");
}
}
}
3、验证发布结果
4、客户端代码生成
# 前置条件 安装并配置apache-cxf环境变量
# 参数说明 -d|目标代码存放位置 -p|源码生成所在包
wsdl2java -encoding utf-8 -d e:\\temp -p com.haotu369.webservice.cxf.client http://127.0.0.1:9082/cxf/service/workflow?wsdl
客户端代码生成图
ps: 客户端代码WorkFowServiceService可删除,其中IP地址固定死,接下来将会介绍,动态IP地址调用
5、客户端调用测试
public class WebServiceTest {
@Test
public void testWebServiceByCXF() {
String address = "http://127.0.0.1:9082/cxf/service/workflow";
JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean();
proxyFactoryBean.setAddress(address); // 动态IP地址设置
proxyFactoryBean.setServiceClass(WorkFlowService.class);
WorkFlowService workFlowService = (WorkFlowService) proxyFactoryBean.create();
workFlowService.execute();
}
}
6、OK, 大功告成!