CXF WebService 范例及与Spring的整合

一、CXF准备工作
jar包下载地址、
jar包结构、
源代码
特性简介:
  代码生成工具:Java to WSDL; WSDL to Java; XSD to WSDL; WSDL to XML; WSDL to SOAP; WSDL to Service;
  支持:JAX-WS, JAX-WSA, JSR181, SAAJ;
  支持:SOAP 1.1/1.2, WS-I BasicProfile, WS-Security, WS-Addressing, WS-RM和WS-Policy;
  支持:WSDL1.1/2.0
  支持:MTOM; javascript,
  通过Yoko支持CORBA;
  通过Tuscany支持SCA;
  通过ServiceMix支持JBI
  内置Jetty应用服务器

二、CXF示例

---------------------------server side-------------------------------------------
Service Main:

1 System.out.println("Server start...");
2 HelloWorldService service = new HelloWorldService();
3 String address = "http://localhost:9000/helloWorld";
4 javax.xml.ws.Endpoint.publish(address, service);
5
6 System.out.println("Server ready...");
7 Thread.sleep(1000*60);
8 System.out.println("Server exiting.");
9 System.exit(0);

Service Implementation:

1 @WebService
2 @SOAPBinding(style = Style.RPC)
3 public class HelloWorldService {
4 public String sayHello(@WebParam(name="name") String name) {
5 return name + " say: Hello World ";
6 }
7 }

---------------------------client side-------------------------------------------
Client Main:

1 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean
2
3 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
4 factory.setServiceClass(IHelloWorldService.class);
5 factory.setAddress("http://localhost:9000/helloworld");
6
7 IHelloWorldService service = (IHelloWorldService)factory.create();
8 System.out.println("[result]" + service.sayHello("hoojo"));

Service Interface:

1 @WebService
2 public interface IHelloWorldService {
3 public String sayHello(@WebParam(name="name") String name);
4 }

 三、CXF对Interceptor拦截器的支持
  CXF的Interceptor和Axis的Handler功能类似。

Client Side:

1 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
2 factory.getInInterceptors().add(new LoggingInInterceptor());
3 factory.getOutInterceptors().add(new LoggingOutInterceptor());

Service Side:
  (自己编写一个Interceptor,继承AbstractPhaseInterceptor类,实现handleMesssage和一个单参数的构造函数)

 1 JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
2 factory.setServiceClass(HelloWorldService.class);
3 factory.setAddress("http://localhost:9000/helloWorld");
4 factory.setServiceBean(new HelloWorldService());
5
6 factory.getInInterceptors().add(new MyMessageInterceptor(Phase.RECEIVE));
7 factory.getOutInterceptors().add(new MyMessageInterceptor(Phase.SEND));
8 factory.create();
9 System.out.println("Server start ......");
10 Thread.sleep(1000*60);
11 System.exit(0);
12 System.out.println("Server exit.");

四、传递复杂类型对象
  (略)

五、CXF整合Spring

1、web.xml中配置信息如下:

<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.conext.ContextLoaderListener</listener-class>
</listener>

<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<servlet-name="CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

2、在applicationContext.xml的bean属性中加入如下信息:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context
="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd
">

3、在applicationContext.xml中添加如下信息:

<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"/>

4、在applicationContext.xml中添加如下信息:

<bean id="userServiceBean"
class
="com.hoo.service.ComplexUserService"/>

<jaxws:server id="userService"
serviceClass
="com.hoo.service.IComplexUserService" address="/Users">
<jaxws:serviceBean>
<ref: bean="userServiceBean"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
...
</jaxws:server>


参考文档:
http://wenku.baidu.com/view/f7f8211e59eef8c75fbfb398.html?from=rec&pos=0&weight=69&lastweight=24&count=5




 

posted @ 2012-01-11 15:21  万法自然~  阅读(379)  评论(0)    收藏  举报