`
human_zx
  • 浏览: 63527 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

利用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);
}
 创建接口的实现类:HelloWorldImpl,如下
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;
    }
}
*@WebService:申明为webservice的注解 
*endpointInterface:要暴露的接口类
 *serviceName :    服务名
在WEB-INF目录下新建beans.xml,如下:
<?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 :接口类的实现类
        address:   要和注释里面神秘的服务名对应,
修改web.xml文件,如下:
<?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 -->
启动tomcat
测试:简单的测试就是ie测试,在浏览器中输入http://localhost:8080/CxfService/services/,如果出现

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

关掉防火墙就可以了。

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

11
1
分享到:
评论
11 楼 pidtaobao 2013-11-21  
对我太有用了,我用的是当前最新的cxf 2.7.7,为了图方便,加入了lib所有的jar包
测试成功通过!
10 楼 xiaoliu66007 2013-08-19  
看了这篇文章,我表示万分感谢
9 楼 a627371545 2013-06-23  
xml的资源在哪
8 楼 ssntingyu 2012-05-06  
你好,我把@WebService的接口和实现都改成hibernate自动生成的函数了,为什么发布输入http://localhost:8080/CxfService/services?wsdl就是说 The requested resource () is not available.呢
7 楼 itcyt 2011-12-23  
怎么加入安全认证呢?
6 楼 qianlixunni 2011-09-29  
讲得简单明了 ,清晰 ,不错
5 楼 binghejinjun 2011-04-21  
wanxuesi 写道
您好:
我在地址栏输入:http://localhost:8000/CxfService/services
显示:
Endpoint address: http://localhost:8000/CxfService/services/HelloWorld
Wsdl: {http://service.cxf.zx.com/}HelloWorld
Target namespace: http://service.cxf.zx.com/

我输入:http://localhost:8000/CxfService/services/HelloWorld?wsdl

就报错,请问是为什么啊?


当我运行client 这个类时,能够显示正确的结果。为什么添加“?wsdl”就报错呢

迫切想要等到您的答复。

   这可能是因为你的tomcat版本问题,你把tomcat换成5.5的试试
4 楼 wdlfellow 2009-02-15  
非常感谢,正巧需要用到这个,cxf官方的文档太复杂了
3 楼 wanxuesi 2008-11-04  
您好:
我在地址栏输入:http://localhost:8000/CxfService/services
显示:
Endpoint address: http://localhost:8000/CxfService/services/HelloWorld
Wsdl: {http://service.cxf.zx.com/}HelloWorld
Target namespace: http://service.cxf.zx.com/

我输入:http://localhost:8000/CxfService/services/HelloWorld?wsdl

就报错,请问是为什么啊?


当我运行client 这个类时,能够显示正确的结果。为什么添加“?wsdl”就报错呢

迫切想要等到您的答复。
2 楼 wdragon1983 2008-07-16  
结果返回就是Response: Hello null。

这是为什么,我看了其他人写的接口类,不是每个都要加类似与(@WebParam(name="text") 的东西吧。
1 楼 wdragon1983 2008-07-16  
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text") String text);
}

这样在客户端,
String response = client.sayHi("Joe");
System.out.println("Response: " + response);
返回结果是 Response: Hello Joe。


但是如果把接口类里面的
(@WebParam(name="text") 去掉,变成

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHi( String text);

}

相关推荐

    在Spring工程里利用CXF实现webservice的demo

    NULL 博文链接:https://uuuvvv.iteye.com/blog/508678

    使用CXF实现WebService

    MyCXFService为WebService服务端,MyCXFClient为客户端调用 ...7、以上第4点为利用Web形式进行调用WebService,还有其它方法调用: 手动执行com.lun.test.Test.java与Test1.java里的main方法,也是调用WebService服务

    Spring2+CXF实现webservice笔记

    利用spring+cxf轻松实现webservice接口

    cxf框架webservice所需所有jar包.zip

    利用cxf创建webservice ,push service 时报异常,该问题主要原因为 cxf 包未,本人是一步一个雷采过来的,一个接一个的异常。后来在网上查到一个博客说包不全,最后重新下载cxf 所有包 build 进项目后不在包...

    cxf开发webservice实践

    利用Apache CXF进行WebService的开发

    cxf构建webservice实例

    cxf构建webservice实例,包含客户端调用测试代码以及完整lib包,即下即用。另InterfaceController.java开始,利用velocity封装了xml与bean模版化转换,希望对大家有用。

    CXF发布webService

    利用cxf发布webservice小例子

    彻底了解|利用Apache CXF框架开发WebService

    CXF就是一个WebService的框架,在生产环境中一般情况下我们都使用框架来开发,这个框架简单的说就是将WebService的开发给简化了,而且还新增了拦截器。本文将带大家利用Apache CXF快速实现一个WebService。

    利用CXF发布restful WebService 研究

    NULL 博文链接:https://zfms.iteye.com/blog/1526829

    spring3 cxf oracle 创建webservice验证 myeclipse10开发

    利用spring3 + cxf 开发的一个webservice验证用户是否存在 数据库oracle ,有建库脚本。下载后改一下datasource中oracle配置 jaxws:endpoint传参研究了好几天,备份一下。

    CXF WEBSERVICE入门,非常详细实用

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在...

    Java调用CXF WebService接口的两种方式实例

    今天小编就为大家分享一篇关于Java调用CXF WebService接口的两种方式实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    java cxf webservice

    利用apache的cxf开发webservice.其中包含用户令牌和数字签名方式加密。 全部现实。并且运行成功。 压缩包中含有数字签名文档,代码类中spring的jdbctemplate操作oracle数据库。

    我个人编写的webservice框架(利用CXF,Maven构建)

    作为一个一年工作经验的人,我自己通过网上搜寻资料,利用CXF,通过Maven构建项目,成功把Spring、 mybaties、CXF整合起来,作为服务器端的接口,供别人调用。最后成功发布接口,经测试没有问题,希望大家共同学习,...

    WebService with Apache CXF

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在...

Global site tag (gtag.js) - Google Analytics