参见文章1和文章2
(1)编写服务程序,需要cxf的包,我用的是cxf2.6.1,都是使用内置jetty发布,方法有两种:
1.使用Sun JAX-WS 2中Endpoint.publish进行发布,包含的库有cxf-api-2.6.1.jar,cxf-rt-bindings-xml-2.6.1.jar,cxf-rt-core-2.6.1.jar,cxf-rt-frontend-jaxrs-2.6.1.jar,cxf-rt-transports-http-2.6.1.jar
编写服务端代码如下:

package com.sysware.app.service.impl;import com.sysware.app.domain.RetInfo;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;@WebService
@SOAPBinding(style = Style.RPC)
public class GetInfoServiceImpl
{@WebMethod(operationName = "add")@WebResult(name = "result")public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2")int num2){return num1 + num2;}@WebMethod(operationName = "getRetInfo")@WebResult(name = "result")public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age")int age){RetInfo retInfo = new RetInfo();retInfo.setAge(age);retInfo.setName(name);return retInfo;}}

发布web service接口

package com.sysware.app.service.inf;public interface IDeployWebService {void deployService(String address);
}

发布web service实现类

package com.sysware.app.service.impl;import com.sysware.app.service.inf.IDeployWebService;import javax.xml.ws.Endpoint;public class DeployWebServicePublishImpl implements IDeployWebService {public DeployWebServicePublishImpl() {}@Overridepublic void deployService(String address) {GetInfoServiceImpl service = new GetInfoServiceImpl();Endpoint.publish(address, service);}
}

主类

package com.sysware.app;import com.sysware.app.service.impl.GetInfoServiceImpl;import javax.xml.ws.Endpoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.sysware.app.service.impl.DeployWebServicePublishImpl;
import com.sysware.app.service.inf.IDeployWebService;public class App {/*** <b>function:</b>发布WebService** @author hoojo*/public static void deployService(String ip, int port) {GetInfoServiceImpl service = new GetInfoServiceImpl();if (ip.length() < 1)ip = "127.0.0.1";if (port < 1)port = 8080;String address = "http://" + ip + ":" + String.valueOf(port) + "/getInfoService";IDeployWebService idlp = new DeployWebServicePunishImpl();idlp.deployService(address);}public static void main(String[] args) throws InterruptedException {String ip = "";int port = 8080;if (args.length > 0) {ip = args[0];if (args.length > 1)port = Integer.parseInt(args[1]);}System.out.println("Server start ……");//发布WebServicedeployService(ip, port);System.out.println("server ready ……");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));while (true) {Thread.sleep(500);try {String a = br.readLine();if ("q".equals(a))break;elseSystem.out.println("invalid input");} catch (IOException e) {e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.}}System.out.println("server exiting");//休眠60秒后就退出System.exit(0);}
}

2.用JaxWsServerFactoryBean发布,需要独立的jetty包,包含的库有cxf-api-2.6.1.jar,cxf-rt-bindings-xml-2.6.1.jar,cxf-rt-core-2.6.1.jar,cxf-rt-frontend-jaxrs-2.6.1.jar,cxf-rt-transports-http-2.6.1.jar,cxf-rt-bindings-soap-2.6.1.jar,cxf-rt-databinding-jaxb-2.6.1.jar,cxf-rt-frontend-jaxws-2.6.1.jar,cxf-rt-frontend-simple-2.6.1.jar,cxf-rt-transports-http-jetty-2.6.1.jar,jetty-continuation-7.6.6.v20120903.jar,jetty-http-7.6.6.v20120903.jar,jetty-io-7.6.6.v20120903.jar,jetty-server-7.6.6.v20120903.jar,jetty-util-7.6.6.v20120903.jar,servlet-api-2.5.jar,wsdl4j-1.6.2.jar,xmlschema-core-2.0.2.jar

发布web service实现类

package com.sysware.app.service.impl;import com.sysware.app.service.inf.IDeployWebService;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class DeployWebServiceJaxwsImpl implements IDeployWebService {@Overridepublic void deployService(String address) {GetInfoServiceImpl impl = new GetInfoServiceImpl();JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();factoryBean.setAddress(address);factoryBean.setServiceClass(GetInfoServiceImpl.class);factoryBean.setServiceBean(impl);factoryBean.create();}
}

将App类中的代码更改

private final static IDeployWebService deployWebServicePublishImpl = new DeployWebServiceJaxwsImpl();

(2)编写客户端程序
运行cxf里面bin目录下wsdl2java

E:\Source\Java\mysource\testcxfclient>E:\Source\Java\apache-cxf-2.6.2\bin\wsdl2java -p com.sysware.app -d e:\Source\Java\mysource\testcxfclient\src\main\java http://192.168.128.234:9111/getInfoService?wsdl

会产生一系列web service接口java文件
-p 指定其wsdl的命名空间,也就是要生成代码的包名
-d 指定要产生代码所在目录

2.包含的库有cxf-api-2.6.1.jar,cxf-rt-bindings-xml-2.6.1.jar,cxf-rt-core-2.6.1.jar,cxf-rt-frontend-jaxrs-2.6.1.jar,cxf-rt-transports-http-2.6.1.jar,cxf-rt-bindings-soap-2.6.1.jar,cxf-rt-databinding-jaxb-2.6.1.jar,cxf-rt-frontend-jaxws-2.6.1.jar,cxf-rt-frontend-simple-2.6.1.jar,cxf-rt-transports-http-jetty-2.6.1.jar,jetty-continuation-7.6.6.v20120903.jar,jetty-http-7.6.6.v20120903.jar,jetty-io-7.6.6.v20120903.jar,jetty-server-7.6.6.v20120903.jar,jetty-util-7.6.6.v20120903.jar,servlet-api-2.5.jar,wsdl4j-1.6.2.jar,xmlschema-core-2.0.2.jar
主类

package com.sysware.app;import com.sysware.app.domain.RetInfo;
import com.sysware.app.service.impl.GetInfoServiceImpl;
import com.sysware.app.service.impl.GetWebServiceJaxwsImpl;
import com.sysware.app.service.inf.IGetWebService;public class ClientApp {public static void main(String[] args) {String ip = "";int port = 8080;if (args.length > 0) {ip = args[0];if (args.length > 1)port = Integer.parseInt(args[1]);}String address = "http://" + ip + ":" + String.valueOf(port) + "/getInfoService";IGetWebService getWebService = new GetWebServiceJaxwsImpl();GetInfoServiceImpl getInfoService = getWebService.getService(address);if (null != getInfoService) {System.out.println("10 + 20 = " + Integer.toString(getInfoService.add(10, 20)));RetInfo retInfo = getInfoService.getRetInfo("johnny", 22);System.out.println("retInfo:" + retInfo.getName());}}
}

获取web service接口类

package com.sysware.app.service.inf;import com.sysware.app.service.impl.GetInfoServiceImpl;public interface IGetWebService {public GetInfoServiceImpl getService(String address);
}

1.通过JaxWsServerFactoryBean调用
获取web service 接口实现类

package com.sysware.app.service.impl;import com.sysware.app.service.inf.IGetWebService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class GetWebServiceJaxwsImpl implements IGetWebService {@Overridepublic GetInfoServiceImpl getService(String address) {JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();soapFactoryBean.setAddress(address);soapFactoryBean.setServiceClass(GetInfoServiceImpl.class);return (GetInfoServiceImpl) soapFactoryBean.create();}
}

2.通过标准JAX_WS API实现

package com.sysware.app.service.impl;import com.sysware.app.service.inf.IGetWebService;import java.net.MalformedURLException;
import java.net.URL;public class GetWebServiceQNameImpl implements IGetWebService {@Overridepublic GetInfoServiceImpl getService(String address) {try {GetInfoServiceImplService getInfoServiceImplService = new GetInfoServiceImplService(new URL(address + "?wsdl"));return getInfoServiceImplService.getGetInfoServiceImplPort();} catch (MalformedURLException e) {e.printStackTrace();return null;}}
}

GetInfoServiceImplService将由wsdl2java自动产生
主类更改如下

IGetWebService getWebService = new GetWebServiceQNameImpl();

cxf 创建webservice相关推荐

  1. CXF创建WebService服务配置说明

    使用CXF创建WebService服务 1.描述 使用CXF创建WebService服务样例,使用CXF创建的服务不需要安装Tomcat也可以启动. 2.开发环境 Eclipse开发工具,JDK1.7 ...

  2. 系统开发系列 之MyEclipse创建WebService详细教程和调用教程(spring框架+maven+CXF框架)

    1 回顾 [系统开发系列 之MyEclipse创建WebService详细教程和调用教程]介绍了使用JWS实现WebService接口的发布和调用,主要涉及的点有: (1)MyEclipse点击Fil ...

  3. CXF之webservice

    使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务 ...

  4. sts 创建webservice项目_常用的RPC架构---WebService

    webService在老项目中经常使用,包括现在有的银行,保险的项目中还在使用.WebService是一种跨平台的rpc技术协议.由SOAP,UDDI,WSDL组成.soap是一种使用xml进行数据编 ...

  5. Java笔记-使用CXF开发WebService服务器

    这里使用CXF开发WebService,要引入下面这个Maven <dependency><groupId>org.apache.cxf</groupId>< ...

  6. cxf开发webservice服务端怎么返回固定的报文格式_Spring boot webservice怎么玩? 第277篇...

    相关历史文章(阅读本文之前,您可能需要先看下之前的系列?) WebService SOAP概述 - 第275篇 WSDL是什么"Lese" - 第276篇 一.前言 当官不为民做主 ...

  7. 13.Axis创建webservice客户端和服务端

    转自:https://blog.csdn.net/chenghui0317/article/details/9318317 一.Axis的介绍 Web Service是现在最适合实现SOA的技术,而A ...

  8. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  9. apache cxf 测试webservice接口

    2019独角兽企业重金招聘Python工程师标准>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

最新文章

  1. Python查找相同元素,不同元素
  2. opencv计算两数组的乘积_opencv矩阵运算(2)
  3. vscode中go插件配置
  4. 手机端适配rem计算方法
  5. 掌握基于AOP事务管理
  6. Spring Data JPA 从入门到精通~@Modifying修改查询
  7. 【算法】算法 动态规划 背包问题
  8. 直方图均衡化原理及c++代码
  9. 兆易创新GD32系列单片机不同容量和启动文件之间的选择(GD32F10X_MD/GD32F10X_HD/GD32F10X_XD/GD32F10X_CL)
  10. Hadoop的安装教程,很详细
  11. linux设置python环境变量
  12. 错误代码:88000, 错误信息:without comment privilege hint: [7oJ0533w689] rid: 630432cd-15944cf6-083e04fc
  13. android进程通信6,[Android]你不知道的Android进程化(6)--进程通信Andromeda框架
  14. OSChina 周四乱弹 —— 我看你TM像病毒
  15. 有意思的shell命令行提示符
  16. SDNU 1221
  17. github优秀项目分享:基于yolov3的轻量级人脸检测、增值税发票OCR识别 等8大项目...
  18. mysql取中间的10个数据_数据库取中间几条记录
  19. VS2017 创建自定义WPF项目模板
  20. python url编码解码_python 实现 urlencode 与 urldecode (中文及特殊字符编解码)

热门文章

  1. 【微信小程序】collection.watch实现对云端数据的实时监控
  2. matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)
  3. Yahoo推出Ymail
  4. toj1746How Many Sums
  5. 百度推广链接与百度排名
  6. 数据挖掘基础:度量数据的相似性和相异性
  7. .html右键不能打开,10种HTML禁止鼠标右键方法,你知道几种?
  8. POJ 3411 DFS
  9. 将数字格式设置为文本格式,并使其出现左上角绿色小三角
  10. 1 Views and Quality Objectives of Software Construction