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:package cn.com.service;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
HelloWorldImpl:
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中配置:
<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 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:
package cn.com.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.service.HelloWorld;
public class client {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-bean.xml");
HelloWorld client = (HelloWorld) ctx.getBean("client");
String result = client.sayHello("你好@@!!");
System.out.println(result);
}
}
spring-bean.xml:
<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>
- 15:15
- 浏览 (696)
- 评论 (1)
- 分类: WebService
- 相关推荐
发表评论
- 浏览: 6748 次
- 性别:

- 来自: 重庆

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
IReport + JasperReport( ...
[u][/u][i][/i]引用[b][/b] [img][/img][u ...
-- by wqmain -
Struts2拦截器
...
-- by irobot -
这就是传说中的地震
slaser 写道ddandyy 写道唯一幸运的就是中心点不是大城市...... ...
-- by slaser -
这就是传说中的地震
ddandyy 写道唯一幸运的就是中心点不是大城市......... 否则就是第 ...
-- by slaser -
这就是传说中的地震
唯一幸运的就是中心点不是大城市......... 否则就是第2个唐山
-- by ddandyy






评论排行榜