《转》利用cxf实现webservice
首先下载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,如下:
- package com.zx.cxf.service;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- String sayHi(@WebParam(name="text") String text);
- }
- package com.zx.cxf.service;
- import javax.jws.WebService;
- import com.zx.cxf.service.HelloWorld;
- @WebService(endpointInterface = "com.zx.cxf.service.HelloWorld",
- serviceName = "HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- public String sayHi(String text) {
- return "Hello " + text;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- START SNIPPET: beans -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <jaxws:endpoint
- id="helloWorld"
- implementor="com.zx.cxf.service.HelloWorldImpl"
- address="/HelloWorld" />
- </beans>
- <!-- END SNIPPET: beans -->
注: implementor :接口类的实现类
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <!-- START SNIPPET: webxml -->
- <web-app>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <display-name>CXF Servlet</display-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
- <!-- END SNIPPET: webxml -->
{http://service.cxf.zx.com/}HelloWorldImplPort ,或者输入http://localhost:8080/CxfService/services/HelloWorld?wsdl,出现wsdl文挡,则说明服务器端配置成功。
可户端测试:
测试类如下:
- package com.zx.cxf.service;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import org.apache.cxf.interceptor.*;
- import com.zx.cxf.service.HelloWorld;
- public class client {
- private client() {
- }
- public static void main(String args[]) throws Exception {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.getInInterceptors().add(new LoggingInInterceptor());
- factory.getOutInterceptors().add(new LoggingOutInterceptor());
- factory.setServiceClass(HelloWorld.class);
- factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld");
- HelloWorld client = (HelloWorld) factory.create();
- String reply = client.sayHi("HI");
- System.out.println("Server said: " + reply);
- }
- }
如果控制台打印出:Server said: Hello HI则说明测试成功。
Ps:如果出现in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader.
关掉防火墙就可以了。
下面是源文件:下载后倒入所需要的包
- CxfService.rar (5.5 KB)
| @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文档的值。 | 
《转》利用cxf实现webservice的更多相关文章
- [置顶] 利用CXF发布webService的小demo
		其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架... 先将相关的jar引进来 ... 
- [转]  WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
		以下文章来自 http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ... 
- 利用CXF生成webservice客户端代码
		一.CXF环境的配置 1.下载CXF的zip包. 2.解压.例如:D:\ITSoft\webserviceClientUtils\cxf\apache-cxf-2.7.17 3.配置环境变量:新建变量 ... 
- 3.使用CXF开发webService
		CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ... 
- 使用CXF发布WebService服务简单实例
		一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ... 
- 使用Spring和Tomcat发布CXF SOAP WebService
		上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ... 
- SpringBoot | 第三十四章:CXF构建WebService服务
		前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ... 
- 使用CXF开发WebService程序的总结(六):结合拦截器使用
		1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ... 
- 使用CXF发布WebService
		这里普及一下WebService和cxf的知识.关于webservice和cxf: WebService.各种提供服务的组件 .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ... 
随机推荐
- 设计模式---组件协作模式之观察者模式(Observer)
			一:概念 Observer模式的作用是当一个对象的状态发生变化时,能够自动通知其他关联对象,自动刷新对象状态 Observer模式提供给关联对象一种同步通信的手段,使得某个对象与依赖他的其他对象之间保 ... 
- n的阶乘-编程2.md
			计算阶乘n!: 注意处理结果溢出 方法: 用数组来存储结果 /** * 计算阶乘n!: 注意处理结果溢出 * 方法: 用数组来存储结果 */ public class PowerN { // Time ... 
- 一张非常强大的OSI七层模型图解。。。
			源自http://www.colasoft.com.cn/download/protocols_map.php,非常适合小白入门,后面罗列出来方便大家浏览记忆...(不经意间看到的,分享一下) OSI ... 
- CM记录-选择合适的硬件
			hadoop的运行环境---hadoop/jvm/os/hw 原则1:主节点的可靠性要好于从节点:NameNode(Yarn-ResourceManager).DataNode(NodeManager ... 
- 小心错误使用EasyUI 让网站性能减半
			先不谈需求,和系统架构,直接上来就被抛来了一个问题----基础性能太差了,一个网页打开要好几秒.我了个天,我听了也简直不敢相信,难道是数据量特别大?还是其中业务逻辑特别复杂? 简单的介绍下,基础系统是 ... 
- Python复习笔记(一)高级变量类型
			目标 列表元组 字典 字符串 公共方法 变量高级 01. 列表 02. 元组 03. 字典 04. 字符串 1)判断类型 - 9 2) 查找和替换 - 7 3) 大小写转换 - 5 4) 文本对齐 - ... 
- 8.SpringBoot 模板引擎 Thymeleaf
			1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ... 
- Tomcat 配置目录
			TOMCAT 1.主目录下有bin,conf,lib,logs,temp,webapps,work 1.bin目录主要是用来存放tomcat的命令 2.conf目录主要是用来存放tomcat的一些配置 ... 
- 第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 _ ... 
- Bootstrap2.x与Bootstrap3.x的区别
			做项目时,有时也会参考别的案例的优秀之处.在用Bootstrap的时候,发现很多项目代码都有区别,在<div>布局class上,有用.span*,有用.col-md-*,实际上是Boots ... 
