`

CXF小例 (转)

    博客分类:
  • 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 >  
分享到:
评论

相关推荐

    Cxf转换器示例

    Web Service Cxf转换器的示例,包含源码和jar包。

    07.处理Map等CXF无法自动转换的复合数据类型的形参和返回值

    07.处理Map等CXF无法自动转换的复合数据类型的形参和返回值

    cxf3.1.7最小包

    cxf3.1.7最小包

    spring+cxf小demo

    spring+cxf 入门demo 包括spring配置,web配置,代码实现,简单易懂

    cxf 小案例 java webservice

    cxf 小案例 java webservice

    apache-cxf-2.6.1最小依赖包

    apache-cxf-2.6.1最小依赖包,自测可用,请自行下载。本来想免费分享给各位,但是分数不能标0。另有cxf-2.7.18版本,链接如下:https://download.csdn.net/download/jxsczqh/10814176

    apache-cxf-2.7.7

    CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载 CXF下载

    CXF客户端支持最少jar

    CXF 客户端最少jar包支持,CXF 客户端最少jar包支持,CXF 客户端最少jar包支持,

    cxf-3.1.5 和 cxf-3.3.13 JAVA7和JAVA8对应CXF资源

    JAVA7和JAVA8对应CXF资源 WebService CXF 用了一天时间找,官网打不开,国内要积分,下下来又永不了。最后终于搞到手,上传上来分享给大家。 jdk版本 CXF版本 java 9及以上 3.3.x java 8 3.x java 7 2.2x --- ...

    CXF源码:CXF_Spring源码

    CXF源码:CXF_Spring源码 CXF源码:CXF_Spring源码

    Spring CXF Restful 实例

    Spring CXF Restful 实例

    apache-cxf-2.7.18最小依赖包

    apache-cxf-2.7.18最小依赖包,自测可用,请自行下载。

    cxf源代码,样例,cxfdemo

    cxf服务端开发

    CXF(Webservice)开发手册

    CXF Webservice 开发手册

    cxf的jar包.rar

    利用Apache CXF开发webservice接口需要用到的jar集合 cxf-core-3.0.15.jar cxf-rt-bindings-soap-3.0.15.jar cxf-rt-bindings-xml-3.0.15.jar cxf-rt-databinding-jaxb-3.0.15.jar cxf-rt-frontend-jaxws-3.0.15.jar...

    用cxf开发webservice

    Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式...

    cxf.xml,cxf-servlet.xml,cxf-extension-soap.xml

    &lt;import resource="classpath:META-INF/cxf/cxf.xml"/&gt; &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/&gt; &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml"/&gt;

    cxf3.1.18.rar

    spring 4.2.0 集成的cxf3.1.18的jar包,cxf-core-3.1.18.jar、cxf-rt-bindings-soap-3.1.18.jar、cxf-rt-databinding-jaxb-3.1.18.jar、cxf-rt-frontend-jaxws-3.1.18.jar、cxf-rt-frontend-simple-3.1.18.jar、cxf-...

    apache-cxf-3.0.4

    Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、...

    apache cxf helloworld小例子

    使用apache cxf开发一个入门小程序

Global site tag (gtag.js) - Google Analytics