spring 3.0 cxf2.7.2整合
1,拷贝spring的 jar
2,拷贝cxf的jar包
jetty不需要了
asm
common-logging
neethi
wsdl4j
xmlschema
cxf
http-*
3,修改web.xml 添加对spring的支持
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INFO/applicationContext.xml</param-value>
</context-param>
<listener-class>
ContentLoadListener
</listener-class>
4,在web.xml添加对cxf的支持
//配置cxf的核心控制器
<servlet>
<servlet-name>cxf
<servlet-class>CXFServlet
</servlet>
//所有来自/ws/*的请求交给cxf处理
<servlet-mapping>
<servlet-name>cxf
<url-pattern>/ws/*
</servlet-mapping>
5,在spring中倒入schema和xml配置
在cxf.jar/schema中可以看到 jaxws.xsd 找到命名空间
在cxf.jar/META-INF/spring.schemas 找到schema-location
http\://cxf.apache.org/schemas/jaxws.xsd=schemas/jaxws.xsd
在cxf.jar/META-INF/这里存在一些xml配置文件
在spring需要导入一些配置文件
<import resourse="classpath:/META-INF/xxx.xml"/>
cxf.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
web应用的类加载路径有两类:
1,WEB-INF/classes目录
2,WEB-INF/lib目录
6,在spring中使用jaxws:endpoint元素来暴露Web Service
implementor指定web service的服务提供者,支持两种形式:
1,直接给服务器提供类名
2,设置为spring容器中的一个bean
<jaxws:endpoint
implementor="xxx.xx.xx.Xxx"
address="/ws" >
</jaxws:endpoint>
第二种方式
<bean id="webServiceName" class="xxx.xx.xx.Xxx" >
<ref bean="customerService" />
</bean>
<jaxws:endpoint
implementor="#webServiceName" #代表使用spring容器中的类
address="/ws" >
<jaxws:inInterceptors>
<bean class="xx.xx.xx.Xxx"/>
<bean class="xx.xx.xx.Xxx"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
cxf和Spring的另外一种整合:在action调用web service
1,复制cxf jar包
2,在spring中配置导入cxf提供的schema,xml配置文件(jaxws)
3,在spring中使用jaxws:client来配置webservice
<jaxws:client id="hw"
serviceClass=""
address="http://localhost:8080/ws_03_server_cxf_spring3.1/ws?wsdl">
<jaxws:outInterceptors>
<bean class="">
<contractor-arg value="admin" />
<contractor-arg value="admin" />
</bean>
</jaxws:outInterceptors>
</jaxws:client>
4,添加拦截器方法一样
服务器端
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXF</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- </servlet>
- <!-- 所有来自/ws/*的请求交给cxf处理 -->
- <servlet-mapping>
- <servlet-name>CXF</servlet-name>
- <url-pattern>/ws/*</url-pattern>
- </servlet-mapping>
- </web-app>
spring配置applicationContext.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:oxm="http://www.springframework.org/schema/oxm"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/oxm
- http://www.springframework.org/schema/oxm/spring-oxm-3.0.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" />
- <!--
- 第一种方式
- <jaxws:endpoint
- implementor="com.kenan.cxf.ws.impl.HelloWorldImpl"
- address="/ws" >
- </jaxws:endpoint> -->
- <!-- 第二种方式 -->
- <bean id="webServiceName" class="com.kenan.cxf.ws.impl.HelloWorldImpl" >
- </bean>
- <!-- #代表使用spring容器中的类 -->
- <jaxws:endpoint
- implementor="#webServiceName"
- address="/ws" >
- <jaxws:inInterceptors>
- <bean class="com.kenan.cxf.auth.AuthInterceptor"/>
- </jaxws:inInterceptors>
- </jaxws:endpoint>
- </beans>
spring客户端
applicationContext.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:oxm="http://www.springframework.org/schema/oxm"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/oxm
- http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <jaxws:client id="hw"
- serviceClass="com.kenan.cxf.ws.HelloWorld"
- address="http://localhost:8080/ws_03_server_cxf_spring3.1/ws/ws?wsdl">
- <jaxws:outInterceptors>
- <bean class="com.kenan.cxf.auth.AuthOutInterceptor">
- <constructor-arg value="admin" />
- <constructor-arg value="admin" />
- </bean>
- </jaxws:outInterceptors>
- </jaxws:client>
- </beans>
测试
- package test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.kenan.cxf.ws.HelloWorld;
- import com.kenan.cxf.ws.impl.HelloWorldImpl;
- import com.sun.security.ntlm.Client;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- HelloWorld hello = context.getBean("hw", HelloWorld.class);
- // HelloWorldImpl helloWorld = new HelloWorldImpl();
- // com.kenan.cxf.ws.HelloWorld hello = helloWorld.getHelloWorldImplPort();
- //拦截器
- // Client client = ClientProxy.getClient(hello);
- // client.getOutInterceptors().add(new AuthOutInterceptor("admin", "admin"));
- // client.getOutInterceptors().add(new LoggingOutInterceptor());
- hello.hello("你好");
- // User user = new User();
- // user.setName("柯南");
- // List<Cat> l = hello.getCatsByUser(user);
- // for(Cat cat:l){
- // System.out.println(cat.getName());
- // }
- // StringCat s = hello.getAllCats();
- // for(Entry entry:s.getEntries()){
- // System.out.println(entry.getKey()+":"+entry.getValue());
- // }
- }
- }
本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1129443