cxf2.13整合spring

一、导入cxf所需jar档如下:

commons-logging-1.1.1.jar  
geronimo-activation_1.1_spec-1.0.2.jar (or Sun’s Activation jar)  
geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)  
geronimo-javamail_1.4_spec-1.6.jar (or Sun’s JavaMail jar)  
geronimo-servlet_2.5_spec-1.2.jar (or Sun’s Servlet jar)  
geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)  
geronimo-jaxws_2.1_spec-1.0.jar (or Sun’s jaxws-api-2.1.jar)  
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)  
jaxb-api-2.1.jar  (jdk1.6自帶,jdk1.6無需加)
jaxb-impl-2.1.12.jar (jdk1.6自帶,jdk1.6無需加)
jetty-6.1.21.jar  
jetty-util-6.1.21.jar  
neethi-2.0.4.jar  
saaj-api-1.3.jar  
saaj-impl-1.3.2.jar  
wsdl4j-1.6.2.jar  
wstx-asl-3.2.8.jar  
XmlSchema-1.4.5.jar  
xml-resolver-1.2.jar  
cxf-2.2.2.jar 
FastInfoset-1.2.2.jar

jaxen-1.1.jar

 

二、server 端:

1.HelloWorld.java

代码:

package com.comwave.xzh.webservice;

import javax.jws.WebService;

/**
 * @author panda_xiong
 * @version 1.0
 */
@WebService
public interface HelloWorld

{
  String say(String hello);
}

 

2.HelloWorldImpl.java

代码:

package com.comwave.xzh.webservice;

import javax.jws.WebService;

/**
 * @author panda_xiong
 * @version 1.0
 */
@WebService(endpointInterface = “com.comwave.xzh.webservice.HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
 public String say(String hello) {
  return “hello ” + hello;
 }

}

 

3.server发布spring配置档server.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

     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=”aegisBean” class=”org.apache.cxf.aegis.databinding.AegisDatabinding” />
    
 <bean id=”jaxWsServiceFactoryBean” class=”org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean”>
  <property name=”wrapped” value=”true” />
  <property name=”dataBinding” ref=”aegisBean” />
 </bean>
   
 <jaxws:endpoint id=”webServiceHelloWorld” implementor=”com.comwave.xzh.webservice.HelloWorldImpl” address=”/HelloWorld”>
  <jaxws:serviceFactory>
  <ref bean=”jaxWsServiceFactoryBean”/>
  </jaxws:serviceFactory>
 </jaxws:endpoint>
</beans>

 

可以将上面<jaxws:endpoint>替换成

 <bean id=”helloWorldImpl” class=”com.comwave.xzh.webservice.HelloWorldImpl” />

 <jaxws:endpoint id=”webServiceHelloWorld” implementor=”#helloWorldImpl” address=”/HelloWorld”/>

 

我们注意到这里引入了两个新的名称空间jaxws、jaxrs,jaxws 是配置SOAP 方式的Web 服务,有jaxws:server、jaxws:endpoint、jaxws:client 三个元素,jaxws:server 和jaxws:endpoint 是等效的,都用于发布Web 服务,出现jaxws:endpoint 的原因是JAX-WS 规范中使用EndPoint 发布Web 服务,CXF 为了和JAX-WS 对应,提供了这个与jaxws:server 功能一样的配置元素;jaxrs是REST 方式的Web 服务,有jaxrs:server、jaxrs:client 两个元素。

 

三、客户端

 

1.客户端spring配置档client.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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd“>

<bean id=”aegisBean” class=”org.apache.cxf.aegis.databinding.AegisDatabinding” />

<bean id=”serviceFactoryBean” class=”org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean”>
  <property name=”dataBinding” ref=”aegisBean” />
</bean>

<bean id=”client” class=”com.comwave.xzh.webservice.HelloWorld” factory-bean=”clientFactory” factory-method=”create” />
<bean id=”clientFactory” class=”org.apache.cxf.frontend.ClientProxyFactoryBean”>
  <property name=”serviceFactory” ref=”serviceFactoryBean” />
  <property name=”serviceClass” value=”com.comwave.xzh.webservice.HelloWorld” />
  <property name=”address” value=”http://localhost:8089/WebService/ws/HelloWorld” />
</bean>
</beans>

 

使用Spring开发SOAP方式的客户端(使用标签)

<jaxws:client id=”client” address=”http://localhost:8089/WebService/ws/HelloWorld” serviceClass=”com.comwave.xzh.webservice.HelloWorld”></jaxws:client>

这里属性serviceClass 指向客户端生成的接口,这里就不能指向一个Bean 的名字,因为接口是不能被实例化的。然后你就可以向访问Spring 容器中的Bean 一样去访问这个CXF 的客户端代理(JaxWsClientProxy),下面是我们在JSP 中书写的调用代码:
IHelloService helloService = (IHelloService) WebApplicationContextUtils.getWebApplicationContext(application).getBean”helloServiceClient”);
out.print(helloService.selectMaxAgeStudent(c1, c2).getName());
从II、III 的示例可以看出借助Spring 在Web 容器中发布、访问Web 服务极其简单。关于具体的<jaxws: … 配置项,大多数都可以从元素、属性的名字上看出其作用,具体解释请参看http://cwiki.apache.org/CXF20DOC/jax-ws-configuration.html

注意:上面8089为tomcat发布端口,而WebService为该项目名称。

 2.ClientTest.java

 

   代码:

package com.comwave.xzh.webservice;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientTest {

 private static ClassPathXmlApplicationContext context;

 public static void main(String[] args) {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { “client.xml” });
  HelloWorld client = (HelloWorld) context.getBean(“client”);
  String response = client.say(“jimmy”);
  System.out.println(response);
 }
}

 

四、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:server.xml,classpath:client.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
 
     <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>/ws/*</url-pattern>
     </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

下一篇:

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *