第一种方法:不用导入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发布和调用的更多相关文章

  1. Apache CXF实现WebService入门教程(附完整源码)

    Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...

  2. Apache CXF使用Jetty发布WebService

    一.概述 Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构.它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的 ...

  3. 使用apache cxf实现webservice服务

    1.在idea中使用maven新建web工程并引入spring mvc,具体可以参考https://www.cnblogs.com/laoxia/p/9311442.html; 2.在工程POM文件中 ...

  4. 使用axis2进行webservice发布与调用

    一.介绍下cxf.axis.axis2区别 新一代的Web Services 框架如Axis2.CXF 都是由现有的项目中逐渐演化而来的,Axis2 是由大家熟悉的Axis 1.x 系列演化过来,而A ...

  5. axis2 webservice 发布、调用与项目集成

    发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...

  6. Apache cxf暴露接口以及客户端调用之WebService初步理解

    在我们真实的项目中,经常会调用别人提供给我们的接口,或者在自己的团队中, restful风格的前后端分离也经常会提供一个后端接口暴露出去供app,或者.net/C/C++程序员去调用,此时就需要使用到 ...

  7. webService发布和调用--Axis2

    一.工具 1.下载 Axis2以及eclipse的Axis2插件.http://axis.apache.org/axis2/java/core/download.cgi 2.axis2-1.7.1-w ...

  8. springmvc 集成apache cxf 开发webservice 示例

    今天需要在springmvc中增加webservice功能,试了多次axis2,和cxf都不行,后来发现在springmvc中最好用cxf集成非常方便,在又一次尝试后终于把demo整合到现有的项目中 ...

  9. 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>>>> ...

随机推荐

  1. Java api 入门教程 之 JAVA的文件操作

    I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...

  2. 查询数据过多页面反应慢引入缓存解决方案(Redis、H2)

      问题:原系统查询接口不支持分页也不可能加入分页支持,导致Ajax查询数据过多,返回数据达到2W多条记录时响应已经极慢,查询功能不要求数据实时性,页面反应速度极慢.体验不好:经排查是由于数据量过大导 ...

  3. 烂泥:【转】rsync命令参数详解

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. rsync安装完毕后,我们可以通过rsync –help查看rysnc命令的使用.如下: 有关rsync的命令格式,在此我们就不多介绍了.如果有想了解的 ...

  4. 关于《Windows程序设计(第五版)》中一个实例程序的疑问

    最近一直在看Charlse Petzold的<Windows程序设计>,作为一个新得不能再新的新手,只能先照着书的抄抄源码了,之前的例子一直都很正常,但昨天遇到一个很诡异的BUG. 先看实 ...

  5. CentOS 6.3下配置LVM(逻辑卷管理)

    一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵 ...

  6. android 查看解压后的.xml文件代码(axmlprinter2)

    axmlprinter2工具下载地址:http://pan.baidu.com/s/1o67eXtS 方法:1.将要查看的.xml文件复制到AXMLPrinter2.jar所在目录2.通过cmd 输入 ...

  7. SQL Learning Notes

    Sams Teach Yourself SQL in 10 Minutes

  8. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  9. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  10. AC日记——矩阵交换行 openjudge 1.8 01

    01:矩阵交换行 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个5*5的矩阵(数学上,一个r×c的矩阵是一个由r行c列元素排列成的矩形阵列),将第n行和第m行交换,输出交换 ...