Apache CXF实现WebService发布和调用
第一种方法:不用导入cxf jars
服务端:
1、 新建Web工程
2、新建接口和实现类、测试类
目录结构图如
下:

接口代码:
package com.cxf.spring.service; import javax.jws.WebMethod;
import javax.jws.WebService; @WebService
public interface IGreetingService { @WebMethod
public String greeting(String name); }
实现类代码:
package com.cxf.spring.service; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public class GreetingServiceImpl implements IGreetingService{ @WebMethod
@Override
public String greeting(@WebParam(name ="name") String name) { return "HI:....."+name;
} }
测试类代码:
package com.cxf.spring.test;
import javax.xml.ws.Endpoint;
import com.cxf.spring.service.GreetingServiceImpl;
public class TestSpring {
public static void main(String[] args) {
pushWS_1();
}
public static void pushWS_1() {
String address = "http://localhost:8100/greeting";
System.out.println("Server start....");
Endpoint.publish(address, new GreetingServiceImpl());
System.out.println("Server ready....");
try {
Thread.sleep(3*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Server exit");
System.exit(0);
}
}
运行测试类:访问:http://localhost:8100/greeting?wsdl

客户端:
1、新建java工程 ,配置CXF环境变量 (下载Apache CXF2.7 )
2、CMD打开命令窗口,运行以下命令,生产客户端代码: wsdl2java.bat -p com.cxf.spting -client -encoding utf-8 -noAddressBinding http://localhost:8100/greeting?wsdl
拷贝到新建java工程的src文件下


运行GreetingServiceImpl_GreetingServiceImplPort_Client.java访问webservice

第二种:
新建web工程 引入cxf依赖包(最小jar)

修改以上测试类代码
package com.cxf.spring.test; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.cxf.spring.service.GreetingServiceImpl;
import com.cxf.spring.service.IGreetingService; public class TestSpring { public static void main(String[] args) {
pushWS_2();
} public static void pushWS_2() { JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
bean.setAddress("http://localhost:8100/greeting");
bean.setServiceClass(IGreetingService.class);
bean.setServiceBean(new GreetingServiceImpl());
bean.create(); System.out.println("Server ready...."); try {
Thread.sleep(3*60*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Server exit");
System.exit(0);
}
运行访问
客户端:按照第一种方法执行。。。
另外两种调用webservice的方法
新建工程 ------测试类 ----- 接口:
package com.cxf.test; import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.cxf.test.client.IGreetingService; public class TestClient_2 { public static void main(String[] args) { pull_1(); pull_2();
} public static void pull_1(){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8100/greeting?wsdl");
try {
Object[] objs = client.invoke("greeting", "张三");
System.out.println(objs[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
} public static void pull_2() { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IGreetingService.class);
factory.setAddress("http://localhost:8100/greeting?wsdl");
IGreetingService service = (IGreetingService) factory.create();
System.out.println(">>>>>>>>Client: " + service.greeting("战士")); } }
package com.cxf.test.client; import javax.jws.WebMethod;
import javax.jws.WebService; @WebService(targetNamespace="http://service.spring.cxf.com/", name = "IGreetingService")
public interface IGreetingService { @WebMethod
public String greeting(String name); }
Apache CXF实现WebService发布和调用的更多相关文章
- Apache CXF实现WebService入门教程(附完整源码)
Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...
- Apache CXF使用Jetty发布WebService
一.概述 Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构.它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的 ...
- 使用apache cxf实现webservice服务
1.在idea中使用maven新建web工程并引入spring mvc,具体可以参考https://www.cnblogs.com/laoxia/p/9311442.html; 2.在工程POM文件中 ...
- 使用axis2进行webservice发布与调用
一.介绍下cxf.axis.axis2区别 新一代的Web Services 框架如Axis2.CXF 都是由现有的项目中逐渐演化而来的,Axis2 是由大家熟悉的Axis 1.x 系列演化过来,而A ...
- axis2 webservice 发布、调用与项目集成
发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...
- Apache cxf暴露接口以及客户端调用之WebService初步理解
在我们真实的项目中,经常会调用别人提供给我们的接口,或者在自己的团队中, restful风格的前后端分离也经常会提供一个后端接口暴露出去供app,或者.net/C/C++程序员去调用,此时就需要使用到 ...
- webService发布和调用--Axis2
一.工具 1.下载 Axis2以及eclipse的Axis2插件.http://axis.apache.org/axis2/java/core/download.cgi 2.axis2-1.7.1-w ...
- springmvc 集成apache cxf 开发webservice 示例
今天需要在springmvc中增加webservice功能,试了多次axis2,和cxf都不行,后来发现在springmvc中最好用cxf集成非常方便,在又一次尝试后终于把demo整合到现有的项目中 ...
- WebService发布与调用问题:expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {http://schemas.xmlsoap.org/wsdl/}definitions
Mailbox===AsYVzdwoY_b6uD s>>>>>>>javax.xml.ws.Service@103bf65 hs>>>> ...
随机推荐
- iOS获取手机型号,类似iphone 7这种 含swift和OC
获取手机设备信息,如name.model.version等,但如果想获取具体的手机型号,如iphone5.5s这种,就需要如下这种 swift: func phonetype () -> Str ...
- 元组tuple
tuple和list的主要区别就是tuple里的元素的是不能改变的,即tuple时immutable的 #创建tuple >>> tupa = (12 , 'ed' , 34) &g ...
- Mac SVN 命令行
Mac自带了SVN命令行,如我的升级到10.10(OSX yosemite)后命令行版本为1.7.10 以下是一些常用命令 1.将文件checkout到本地目录 svn checkout path(p ...
- 错误 未能找到类型或命名空间名称 (是否缺少 using 指令或程序集引用?)
有时发现,明明引用了,结果却提示未引用, 这时就有可能是两个程序集的目标框架类型不一致导致的(在程序集属性面板里改下即可).
- 在Windows下配置Python+Django+Eclipse开发环境
一.配置开发环境我的开发环境是:Python2.6.7 + Django1.6.2 + Eclipse1.安装Python2.安装Eclipse的Python插件PyDev如上两步如何操作请点击此进行 ...
- How to stop uwsgi when no pidfile in config?
原文: how-to-stop-uwsgi-when-no-pidfile-in-config Q: I've just installed uwsgi by pip install uwsgi in ...
- Django初体验(一):自定义表单提交
注:本人使用的Django1.8.3版本进行测试 除了使用Django内置表单,有时往往我们需要自定义表单.对于自定义表单Post方式提交往往会带来由CSRF(跨站请求伪造)产生的错误"CS ...
- java报表工具FineReport的SQL编辑框的语法简介
感谢大家捧场,这里继续分享关于SQL编辑框的一些语法心得总结,因为数据集定义的面板,也是FineReport报表中最常用的模块之一. 1.我理解的执行过程. 这里其实是生成一个字符串,FineRepo ...
- HashMap的工作原理深入再深入
前言 首先再次强调hashcode (==)和equals的真正含义(我记得以前有人会说,equals是判断对象内容,hashcode是判断是否相等之类): equals:是否同一个对象实例.注意,是 ...
- java 22 - 22 多线程之 匿名内部类的方式实现多线程
首先回顾下之前的匿名内部类: 匿名内部类的格式: new 接口或者接口名(){ 重写方法 }; 本质:是该类或者接口的子类对象 匿名内部类方式使用多线程 1.new Thread(){代码-}.sta ...