第一种方法:不用导入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. [转]Shell中read的常用方式

    原文:Linux Shell Scripting Tutorial V2.0 read命令的语法: read -p "Prompt" variable1 variable2 var ...

  2. jstl 中<c:forEach />标签

    <c:forEach>标签有如下属性: 属性 描述 是否必要 默认值 items 要被循环的信息 否 无 begin 开始的元素(0=第一个元素,1=第二个元素) 否 0 end 最后一个 ...

  3. 工作中常用的Linux命令:ipcs/ipcrm命令

    本文链接:http://www.cnblogs.com/MartinChentf/p/6057100.html (转载请注明出处) ipcs 1. 命令格式 ipcs [resource-option ...

  4. Struts2开发环境搭建,及一个简单登录功能实例

    首先是搭建Struts2环境. 第一步 下载Struts2去Struts官网 http://struts.apache.org/ 下载Struts2组件.截至目前,struts2最新版本为2.3.1. ...

  5. vi/vim实用命令

    查找 n是下一个,N是上一个 撤销和重做 u:撤销上一步的操作 ctrl+r:恢复上一步被撤销的操作 替换 :1,$ s/aaa/bbb/g

  6. 150922-写写博客监督下不自觉的自己-PPT,Linux,HTML

    开始学PHP的日子里,总是懒散的有一天没一天的.无意间听闻写博客来展示代码(现在还远远做不到哇),来监督个人每天的学习进度,鉴于自己还是爱写一点文字,但愿可以坚持下去. 凡是都喜欢有个计划,骨子里的理 ...

  7. stm32中断无电平触发的解决办法

    这几天在用stm32读取FPGA中FIFO里的数据,遇到了不少的问题.其中有个自己觉得比较好玩的问题,就拿出来写写.其实这个问题也比较简单,开始我觉得没必要拿出来写,不过,想想后觉得还是写写吧,就当做 ...

  8. UWP数据绑定

    [ 已针对 Windows 10 上的 UWP 应用更新. 有关 Windows 8.x 文章,请参阅存档 ] 数据绑定是你的应用 UI 用来显示数据的一种方法,可以选择与该数据保持同步. 借助数据绑 ...

  9. 转:Configure your eclipse for C++

    from: http://omtlab.com/configure-your-eclipse-for-c/ Configure your eclipse for C++ July 7, 2013 Th ...

  10. css3和原生js时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...