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>>>> ...
随机推荐
- mysql学习笔记(一)
my建表操作 创建表 create Table <表名> ( 字段名1,数据类型 [列级约束] [默认值], 字段名2,数据类型 [列级约束] [默认值], ... [表级约束], [co ...
- php cli方式下获取服务器ip
(未整理....) (1)php cli方式下获取服务器ip [php] function getServerIp(){ $ss = exec('/sbin/ifconfig et ...
- 设置输入域(input/textarea)中文本光标的位置
以前记录了一篇 将光标定位于输入框最右侧的实现方式 ,实现光标定位在文本的最末.这种需求往往在修改现有的文本.有时可能还需要把光标定位在首位,或者中间某个位置,这就需要实现一个更通用的方法. 这个方法 ...
- [Computer structure] Written Notes
To some extent, taking notes using my pen and pencil is also an interesting thing! bingo! ~ 2016-03- ...
- NYOJ-取石子
(一) 描述一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作石子.游戏的规则是这样的.设有一堆石子,数量为N(1<=N<=1000000), ...
- UVA 10375 Choose and divide【唯一分解定理】
题意:求C(p,q)/C(r,s),4个数均小于10000,答案不大于10^8 思路:根据答案的范围猜测,不需要使用高精度.根据唯一分解定理,每一个数都可以分解成若干素数相乘.先求出10000以内的所 ...
- CSS样式----图文详解:css样式表和选择器
主要内容 CSS概述 CSS和HTML结合的三种方式:行内样式表.内嵌样式表.外部样式表 CSS四种基本选择器:标签选择器.类选择器.ID选择器.通用选择器 CSS三种扩展选择器:组合选择器.后代选择 ...
- CF 371B Fox Dividing Cheese[数论]
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Tree
//Header.h #ifndef _HEAD_ #define _HEAD_ #include <queue> #include <iostream> using name ...
- jquery的ready方法(DOM是否加载完)详解与使用
jquery的ready方法(准备DOM触发)还是比较复杂的,我们先看流程图: