Apache CXF实现的SOAP形式的webservices

前言:常用的web service有两种形式,SOAP和RESTful。这里先说SOAP的实现步骤,RESTful的形式后面再说

一 建项目,导jar包

(1)项目结构

(2)在eclipse中新建一个动态web项目“CXFDemo”,并导入cxf中的jar包(路径:/WEB-INF/lib/)。当然,可以去官网下载,也可以直接使用我用过的jar包,链接:http://pan.baidu.com/s/1pKyelAV

(3)配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>CXFDemo</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/classes/service-beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

二 web service服务端类

(1)接口CXFService.java:

package cn.zifangsky;

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

@WebService
public interface CXFService {
	@WebMethod
	public String sayHello(@WebParam(name="name") String name);
}

说明:
@WebService:标记表示该接口是一个WebService服务
@WebMethod:标记表示WebService中的方法
@WebParam(name=”paramName”)表示方法中的参数,name属性限制了参数的名称,若没有指定该属性,参数将会被重命名

(2)具体的实现类CXFServiceImpl.java:

package cn.zifangsky.impl;

import cn.zifangsky.CXFService;

public class CXFServiceImpl implements CXFService {

	public String sayHello(String name) {
		return "Hello," + name + "\n" + Test.getExtraMessage();
	}

}

注:Test.java类:

package cn.zifangsky.impl;

public class Test {
	public static String getExtraMessage(){
		return "测试";
	}
}

(3)配置service-beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap"
	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">

	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature" />
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<jaxws:server id="sayHelloServices" serviceClass="cn.zifangsky.CXFService" address="/soap/sayHello" >
		<jaxws:serviceBean>
			<bean class="cn.zifangsky.impl.CXFServiceImpl" />
		</jaxws:serviceBean>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:features>
			<ref bean="loggingFeature" />
			<wsa:addressing xmlns:wsa="http://cxf.apache.org/ws/addressing" />
		</jaxws:features>

	</jaxws:server>

</beans>

说明:

i)3个bean配置的是日志,在这里把日志相关的配置删掉也不影响web service服务的正常使用

ii)address指的是发布后这个服务的路径问题,比如说我这里就是:http://localhost:8080/CXFDemo/soap/sayHello

(4)测试

发布这个项目到tomcat,运行之后的效果如下:

点击那个链接,可以发现sayHello这个服务的地址是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl

到此,这个web service的服务端已经配置完成

三 web service客户端配置

服务端提供服务,而客户端使用服务端提供的服务,因此客户端只需要调用服务端提供的服务就可以了

(1)客户端的项目结构:

(2)同样是新建一个动态web工程,然后引入cxf的jar包,同时需要把服务端的CXFService接口打包成一个jar包,然后导入到客户端的lib中,链接:http://pan.baidu.com/s/1eQVWups

(3)客户端测试类ClientDemo.java:

package cn.zifangsky.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.zifangsky.CXFService;

public class ClientDemo {
	public static void main(String args[]){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});

		CXFService clientHello = (CXFService) context.getBean("clientHello");
		String response = clientHello.sayHello("javaee");
		context.close();

		System.out.println("Response: " + response);   
        System.exit(0);  
	} 

}

(4)客户端配置文件client-beans.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:jaxws="http://cxf.apache.org/jaxws"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
       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">  
         
    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature" />
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />      
    <jaxws:client id="clientHello" serviceClass="cn.zifangsky.CXFService" address="http://localhost:8080/CXFDemo/soap/sayHello?wsdl">
    	<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:features>
			<ref bean="loggingFeature" />
			<wsa:addressing xmlns:wsa="http://cxf.apache.org/ws/addressing" />
		</jaxws:features>
    </jaxws:client>   
</beans>

说明:

这里的address需要填写服务的wsdl绝对地址,因此这里就是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl

(5)测试

直接使用“Java Application”运行ClientDemo,输出如下:

---------------------------
ID: 1
Address: http://localhost:8080/CXFDemo/soap/sayHello?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Action xmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHello</Action><MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</MessageID><To xmlns="http://www.w3.org/2005/08/addressing">http://localhost:8080/CXFDemo/soap/sayHello?wsdl</To><ReplyTo xmlns="http://www.w3.org/2005/08/addressing"><Address>http://www.w3.org/2005/08/addressing/anonymous</Address></ReplyTo></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://zifangsky.cn/"><name>javaee</name></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
三月 14, 2016 4:51:41 下午 org.apache.cxf.services.CXFServiceService.CXFServicePort.CXFService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Mon, 14 Mar 2016 08:51:41 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Action xmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHelloResponse</Action><MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:504f4c19-5df9-42bd-bb02-761da4932490</MessageID><To xmlns="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous</To><RelatesTo xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</RelatesTo></soap:Header><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://zifangsky.cn/"><return>Hello,javaee
测试</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
三月 14, 2016 4:51:41 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org[email protected]62a76581: startup date [Mon Mar 14 16:51:39 GMT+08:00 2016]; root of context hierarchy
Response: Hello,javaee
测试

可以发现,最后成功输出了

Response: Hello,javaee
测试

因为我们在客户端中只引用了CXFService.java这个接口,但是却输出了预期的内容,说明客户端的确是调用了服务端的服务并成功返回信息的

时间: 2024-08-07 17:00:29

Apache CXF实现的SOAP形式的webservices的相关文章

Apache CXF实现的RESTful形式的webservices

我在上一篇文章(http://www.zifangsky.cn/2016/03/apache-cxf实现的soap形式的webservices/)中说了下如何实现SOAP方式的web service,这篇文章将继续说明如何实现RESTful形式的web service (1)项目结构: 在这里,我没有新建一个项目,而是在上一个项目的基础上实现的,具体来说就是新建了3个文件,如下图所示: 其中,User类是一个普通的实体类,RestService和RestServiceImpl类分别表示服务接口和

使用Apache CXF开发WebServices服务端、客户端

在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = Celtix + XFire. CXF 继承了 Celtix 和 XFire 两大开源项目的精华, 提供了对 JAX-WS 全面的支持,并且提供了多种 Binding .DataBinding.Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code Fi

Error creating bean with name &#39;org.apache.cxf.binding.soap.customEditorConfigurer&#39;

1.错误描述 2015-09-20 17:08:36 [localhost-startStop-1] WARN org.apache.cxf.bus.spring.BusApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error

Apache CXF 102 CXF with REST

前言 续上篇Apache CXF 101,摘抄部分REST概念性知识,以运行实例考察CXF对REST的支持. 目录 1 REST简介 2 工具 3 运行实例 内容 本Spike记录中内容,如无特别指出,均引用[1]. 1 REST简介 有两种实现Web Service的主要方式:SOAP(Simple Object Access Protocl)和REST架构风格(Representational State Transfer architecture style). 用REST架构风格构建的服

Apache CXF自定义拦截器

为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器,出拦截器. 3.按定义者分:系统拦截器,自定义拦截器. 客户端添加日志拦截器 package com.client.interceptor; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf

Apache cxf 整合 Spring MVC

1.添加依赖pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.o

Apache CXF框架结构和基本原理(转)

原文链接:http://blog.sina.com.cn/s/blog_6182547f01017pak.html CXF旨在为服务创建必要的基础设施,它的整体架构主要由以下几个部分组成: 1.Bus 它是C X F架构的主干,为共享资源提供了一个可配置的场所,作用非常类似于S p r i n g的ApplicationContext.这些共享资源包括WSDL管理器.绑定工厂等.通过对Bus进行扩展,可以方便地容纳自己的资源,或替换现有的资源.默认Bus实现是基于Spring的,通过依赖注入,将

Apache CXF实现WebService开发(一)

Apache CXF简介 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 Services 可以支持多种协议,比如:SOAP.XML/HTTP.RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP.JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝

apache CXF wsdl2java工具的使用

cxf的wsdl2java命令和JDK的wsimport命令的区别和使用 JDK提供了一个wsimport.exe的命令,主要是用于将WebService生成客户端代码,然后好调用WebService. wsimport 是根据JDK1.6.0_21及以上的生成本地代码的,它只能解析服务器端的SOAP协议为1.1,不能解析SOAP1.2的协议.如果解析SOAP1.2 将会解析不完全. 用法:前提是已经将JDK配置为了path环境变量 C:\Documents and Settings\Admin