首先下载cxf包,目前最新的版本是apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html

1. 首先新建一个web工程CxfService,倒入cxf所学要的包。要倒入的包如下:

commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxws-api-2.1.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar

The Spring jars (optional - for XML Configuration support):

aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar

And the CXF jar:

cxf-2.1.jar

2.新建一个接口类:HelloWorld,如下:

  1. package com.zx.cxf.service;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface HelloWorld {
  6. String sayHi(@WebParam(name="text") String text);
  7. }
 创建接口的实现类:HelloWorldImpl,如下
  1. package com.zx.cxf.service;
  2. import javax.jws.WebService;
  3. import com.zx.cxf.service.HelloWorld;
  4. @WebService(endpointInterface = "com.zx.cxf.service.HelloWorld",
  5. serviceName = "HelloWorld")
  6. public class HelloWorldImpl implements HelloWorld {
  7. public String sayHi(String text) {
  8. return "Hello " + text;
  9. }
  10. }
*@WebService:申明为webservice的注解 
*endpointInterface:要暴露的接口类
 *serviceName :    服务名
在WEB-INF目录下新建beans.xml,如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- START SNIPPET: beans -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:jaxws="http://cxf.apache.org/jaxws"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  9. <import resource="classpath:META-INF/cxf/cxf.xml" />
  10. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  11. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  12. <jaxws:endpoint
  13. id="helloWorld"
  14. implementor="com.zx.cxf.service.HelloWorldImpl"
  15. address="/HelloWorld" />
  16. </beans>
  17. <!-- END SNIPPET: beans -->

注: implementor :接口类的实现类

        address:   要和注释里面神秘的服务名对应,
修改web.xml文件,如下:
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE web-app
  3. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  4. "http://java.sun.com/dtd/web-app_2_3.dtd">
  5. <!-- START SNIPPET: webxml -->
  6. <web-app>
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>/WEB-INF/beans.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>
  13. org.springframework.web.context.ContextLoaderListener
  14. </listener-class>
  15. </listener>
  16. <servlet>
  17. <servlet-name>CXFServlet</servlet-name>
  18. <display-name>CXF Servlet</display-name>
  19. <servlet-class>
  20. org.apache.cxf.transport.servlet.CXFServlet
  21. </servlet-class>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>CXFServlet</servlet-name>
  26. <url-pattern>/services/*</url-pattern>
  27. </servlet-mapping>
  28. </web-app>
  29. <!-- END SNIPPET: webxml -->
启动tomcat
测试:简单的测试就是ie测试,在浏览器中输入http://localhost:8080/CxfService/services/,如果出现

{http://service.cxf.zx.com/}HelloWorldImplPort ,或者输入http://localhost:8080/CxfService/services/HelloWorld?wsdl,出现wsdl文挡,则说明服务器端配置成功。

可户端测试:

测试类如下:

  1. package com.zx.cxf.service;
  2. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  3. import org.apache.cxf.interceptor.*;
  4. import com.zx.cxf.service.HelloWorld;
  5. public  class client {
  6. private client() {
  7. }
  8. public static void main(String args[]) throws Exception {
  9. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  10. factory.getInInterceptors().add(new LoggingInInterceptor());
  11. factory.getOutInterceptors().add(new LoggingOutInterceptor());
  12. factory.setServiceClass(HelloWorld.class);
  13. factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld");
  14. HelloWorld client = (HelloWorld) factory.create();
  15. String reply = client.sayHi("HI");
  16. System.out.println("Server said: " + reply);
  17. }
  18. }

如果控制台打印出:Server said: Hello HI则说明测试成功。

Ps:如果出现in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader.

关掉防火墙就可以了。

下面是源文件:下载后倒入所需要的包

@WebService和@WebMethod 是WSDL映射Annotation.这些Annotation将描述Web Service的WSDL文档元素和JAVA源代码联系在一起。
@WebService和@WebMethod WSDL映射Annotation.这些Annotation将描述Web Service的WSDL文档元素和JAVA源代码联系在一起。
@WebService annotation的元素name,serviceName和targetNamespace成员用来描述wsdl:protType,wsdl:service,和targetNameSpace生成WebService中的WSDL文件。
@SOAPBinding 是一个用来描述SOAP格式和RPC的协议的绑定Annotation.
@WebMethod Annotation的operationName成员描述了wsdl:operation,而且它的操作描述了WSDL文件中的SOAPAction头部,这是客户端必须要放入到SOAPHeader中的数值,SOAP1.1中的一种约束。
@WebParam 是一个用来描述SOAP格式和RPC的协议的绑定Annotation.
@WebResult Annotation的partName成员描述了wsdl:part用来返回WSDL文档的值。

转自:http://rargers.iteye.com/blog/196121

《转》利用cxf实现webservice的更多相关文章

  1. [置顶] 利用CXF发布webService的小demo

    其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架... 先将相关的jar引进来 ...

  2. [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

    以下文章来自   http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...

  3. 利用CXF生成webservice客户端代码

    一.CXF环境的配置 1.下载CXF的zip包. 2.解压.例如:D:\ITSoft\webserviceClientUtils\cxf\apache-cxf-2.7.17 3.配置环境变量:新建变量 ...

  4. 3.使用CXF开发webService

    CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...

  5. 使用CXF发布WebService服务简单实例

    一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...

  6. 使用Spring和Tomcat发布CXF SOAP WebService

    上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...

  7. SpringBoot | 第三十四章:CXF构建WebService服务

    前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ...

  8. 使用CXF开发WebService程序的总结(六):结合拦截器使用

    1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ...

  9. 使用CXF发布WebService

    这里普及一下WebService和cxf的知识.关于webservice和cxf:   WebService.各种提供服务的组件     .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...

随机推荐

  1. 设计模式---组件协作模式之观察者模式(Observer)

    一:概念 Observer模式的作用是当一个对象的状态发生变化时,能够自动通知其他关联对象,自动刷新对象状态 Observer模式提供给关联对象一种同步通信的手段,使得某个对象与依赖他的其他对象之间保 ...

  2. n的阶乘-编程2.md

    计算阶乘n!: 注意处理结果溢出 方法: 用数组来存储结果 /** * 计算阶乘n!: 注意处理结果溢出 * 方法: 用数组来存储结果 */ public class PowerN { // Time ...

  3. 一张非常强大的OSI七层模型图解。。。

    源自http://www.colasoft.com.cn/download/protocols_map.php,非常适合小白入门,后面罗列出来方便大家浏览记忆...(不经意间看到的,分享一下) OSI ...

  4. CM记录-选择合适的硬件

    hadoop的运行环境---hadoop/jvm/os/hw 原则1:主节点的可靠性要好于从节点:NameNode(Yarn-ResourceManager).DataNode(NodeManager ...

  5. 小心错误使用EasyUI 让网站性能减半

    先不谈需求,和系统架构,直接上来就被抛来了一个问题----基础性能太差了,一个网页打开要好几秒.我了个天,我听了也简直不敢相信,难道是数据量特别大?还是其中业务逻辑特别复杂? 简单的介绍下,基础系统是 ...

  6. Python复习笔记(一)高级变量类型

    目标 列表元组 字典 字符串 公共方法 变量高级 01. 列表 02. 元组 03. 字典 04. 字符串 1)判断类型 - 9 2) 查找和替换 - 7 3) 大小写转换 - 5 4) 文本对齐 - ...

  7. 8.SpringBoot 模板引擎 Thymeleaf

    1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ...

  8. Tomcat 配置目录

    TOMCAT 1.主目录下有bin,conf,lib,logs,temp,webapps,work 1.bin目录主要是用来存放tomcat的命令 2.conf目录主要是用来存放tomcat的一些配置 ...

  9. 第17月第28天 python yield

    1. class Fab(object): def __init__(self, max): self.max = max self.n, self.a, self.b = 0, 0, 1 def _ ...

  10. Bootstrap2.x与Bootstrap3.x的区别

    做项目时,有时也会参考别的案例的优秀之处.在用Bootstrap的时候,发现很多项目代码都有区别,在<div>布局class上,有用.span*,有用.col-md-*,实际上是Boots ...