CXF小例

现在使用的最流行的webservice框架基本上就是Axis,Axis2和Cxf,其实这些框架间这有各的特点,并不能一概而论哪个好,哪个不好,关键是要根据你的系统自身去选择,看哪个更适合于你。

以下是Axis2和Cxf的部分比较:

1.CXF支持WS-Addressing,WS-Policy,WS-RM,WS-Security 和 WS-I 基本规范。Axis2支持除WS-Policy之外的所有协议,WS-Policy也会在即将到来的版本支持。

2.CXF很容易和Spring集成,而Axis2不。

3.Axis2 支持范围更大的数据绑定,包括XMLBeans、JiBX、JaxMe和JaxBRI同时还有它自带的数据绑定方式--ADB。注意对JaxME和 JaxBRI的支持在Axis2 1.2中仍然处在实验阶段。CXF目前仅支持JAXB和Aegis,对XMLBeans、JiBX和Castor的支持将会在CXF 1.2中实现。

4.Axsi2支持多种语言--在java版本之外还有C/C++的版本可用。

5.Axis2有利于web services的独立,不依赖其他应用,Cxf可以更好的嵌入到原有系统中,专注于开发者的高效和可嵌入性。

 

服务器端:

 

导入包(jaxws-api-2.0.jar在服务器端不是必须的):

aopalliance-1.0.jar
commons-logging-1.1.jar
cxf-2.0.2-incubator.jar
geronimo-activation_1.1_spec-1.0-M1.jar
geronimo-annotation_1.0_spec-1.1.jar
geronimo-javamail_1.4_spec-1.0-M1.jar
geronimo-servlet_2.5_spec-1.1-M1.jar
geronimo-ws-metadata_2.0_spec-1.1.1.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.2.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-core-2.0.4.jar
spring-web-2.0.4.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
xml-resolver-1.2.jar
XmlSchema-1.2.jar

 

新建一个web工程cxf,分别编写接口HelloWorld和其实现类HelloWorldImpl,接口暴露出方法sayHello()供客户端调用(注,必须使用jdk1.5进行编译):

HelloWorld:

Java代码
  1. package  cn.com.service;  
  2.   
  3. import  javax.jws.WebService;  
  4.   
  5. @WebService   
  6. public   interface  HelloWorld {  
  7.   
  8.     public  String sayHello(String text);  
  9. }  
package cn.com.service;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

	public String sayHello(String text);
}

 

 

 HelloWorldImpl:

Java代码
  1. package  cn.com.service;  
  2.   
  3. import  javax.jws.WebService;  
  4.   
  5. @WebService (endpointInterface= "cn.com.service.HelloWorld" )  
  6. public   class  HelloWorldImpl  implements  HelloWorld {  
  7.   
  8.     public  String sayHello(String text) {  
  9.           
  10.         return   "Hello"  + text ;  
  11.     }  
  12.   
  13. }  
package cn.com.service;

import javax.jws.WebService;

@WebService(endpointInterface="cn.com.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	public String sayHello(String text) {
		
		return "Hello" + text ;
	}

}

 

 

在spring的配置文件spring-bean.xml中配置:

Xml代码
  1. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  2.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:jaxws = "http://cxf.apache.org/jaxws"   
  4.     xsi:schemaLocation ="   
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  7.   
  8.     < import   resource = "classpath:META-INF/cxf/cxf.xml"   />   
  9.     < import   resource = "classpath:META-INF/cxf/cxf-extension-soap.xml"   />   
  10.     < import   resource = "classpath:META-INF/cxf/cxf-servlet.xml"   />   
  11.   
  12.     < bean   id = "hello"   class = "cn.com.service.HelloWorldImpl"   />   
  13.   
  14.     < jaxws:endpoint   id = "helloWorld"   implementor = "#hello"   
  15.         address = "/HelloWorld"   />   
  16.           
  17.           
  18. </ 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" />

	<bean id="hello" class="cn.com.service.HelloWorldImpl" />

	<jaxws:endpoint id="helloWorld" implementor="#hello"
		address="/HelloWorld" />
		
		
</beans>

 

web.xml:

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   version = "2.4"   xmlns = "http://java.sun.com/xml/ns/j2ee"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  6.     < context-param >   
  7.         < param-name > contextConfigLocation </ param-name >   
  8.         < param-value > classpath:spring-bean.xml </ param-value >   
  9.     </ context-param >   
  10.   
  11.     < listener >   
  12.         < listener-class >   
  13.             org.springframework.web.context.ContextLoaderListener  
  14.         </ listener-class >   
  15.     </ listener >   
  16.       
  17.     < servlet >   
  18.         < servlet-name > CXFServlet </ servlet-name >   
  19.         < display-name > CXF Servlet </ display-name >   
  20.         < servlet-class >   
  21.             org.apache.cxf.transport.servlet.CXFServlet  
  22.         </ servlet-class >   
  23.         < load-on-startup > 1 </ load-on-startup >   
  24.     </ servlet >   
  25.   
  26.     < servlet-mapping >   
  27.         < servlet-name > CXFServlet </ servlet-name >   
  28.         < url-pattern > /* </ url-pattern >   
  29.     </ servlet-mapping >   
  30. </ web-app >   
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-bean.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>/*</url-pattern>
	</servlet-mapping>
</web-app>

 

将应用部署到web服务器中(我是部署到tomcat下),可以通过http://localhost:8080/cxf/HelloWorld?wsdl 检测是否发布成功.

 

客户端:

 

导入的包同服务器端相同(只是要将服务器端的接口类打成jar包,方便在客户端调用)

client:

 

Java代码
  1. package  cn.com.client;  
  2.   
  3. import  org.springframework.context.ApplicationContext;  
  4. import  org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import  cn.com.service.HelloWorld;  
  7.   
  8. public   class  client {  
  9.   
  10.     public   static   void  main(String[] args) {  
  11.   
  12.         ApplicationContext ctx = new  ClassPathXmlApplicationContext(  
  13.                 "spring-bean.xml" );  
  14.         HelloWorld client = (HelloWorld) ctx.getBean("client" );  
  15.         String result = client.sayHello("你好@@!!" );  
  16.         System.out.println(result);  
  17.     }  
  18. }  

 

 

spring-bean.xml:

Xml代码
  1. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  2.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:jaxws = "http://cxf.apache.org/jaxws"   
  4.     xsi:schemaLocation ="   
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  7.   
  8.     < import   resource = "classpath:META-INF/cxf/cxf.xml"   />   
  9.     < import   resource = "classpath:META-INF/cxf/cxf-extension-soap.xml"   />   
  10.     < import   resource = "classpath:META-INF/cxf/cxf-servlet.xml"   />   
  11.   
  12.     < bean   id = "hello"   class = "cn.com.service.HelloWorldImpl"   />   
  13.   
  14.     < jaxws:endpoint   id = "helloWorld"   implementor = "#hello"   
  15.         address = "/HelloWorld"   />   
  16.           
  17.           
  18. </ beans >  

你可能感兴趣的:(spring,xml,bean,servlet,webservice)