Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)

来源:互联网 发布:电路模拟仿真软件 编辑:程序博客网 时间:2024/06/07 05:11

CXFSpring整合,分两个方面给大家介绍:

 

1,在传统ssh项目基础上添加Web Service

赋值CXFjar

web.xml配置文件中导入CXF的核心控制器:CXFServlet

Spring配置文件中导入CXF提供Schemaxml配置文件

Spring配置文件中使用jaxws:endpoint元素来暴露Web Service

如果要添加拦截器,在jaxws:endpoint元素里添加

inInterceptors,outInterceptors子元素

2,远程调用Web Service服务(让Action依赖远程Web Service的接口)

复制CXFjar

Spring配置文件中导入CXF提供Schemaxml配置文件

Spring配置文件中使用jaxws:client元素来配置远程Web Service代理

如果要添加拦截器。在jaxws:client元素里添加

inInterceptors,outInterceptors子元素

 

第一个方面:提供Web Service的服务


服务端:

 

项目结构:




在相应的SSH项目中添加CXF的相关jar包:




Web.xml


[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     version="2.5">  
  6.     <listener>    
  7.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  8.     </listener>    
  9.     
  10.     <context-param>    
  11.         <param-name>contextConfigLocation</param-name>    
  12.         <param-value>/WEB-INF/applicationContext-common.xml</param-value>    
  13.     </context-param>    
  14.       <!--所有来自/*的请求,都交由 CXFServlet来处理-->  
  15.     <servlet>    
  16.         <servlet-name>HelloWorldService</servlet-name>    
  17.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  18.         <load-on-startup>1</load-on-startup>    
  19.     </servlet>    
  20.     <servlet-mapping>    
  21.         <servlet-name>HelloWorldService</servlet-name>    
  22.         <url-pattern>/*</url-pattern>    
  23.     </servlet-mapping>    
  24.      
  25. </web-app>  

applicationContext-common.xml:


[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"    
  5.     xsi:schemaLocation="    
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    
  8.   
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />    
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
  12.     
  13.     <bean id="helloWorld" class="com.tgb.service.impl.HelloWorldImpl">  
  14.         <property name="userService" ref="userService"></property>  
  15.     </bean>    
  16.     
  17.   <!-- 用户的Service -->      
  18.     <bean id="userService" class="com.tgb.service.impl.UserServiceImpl">         
  19.     </bean>  
  20.     <!-- implementor指定webservice的服务提供者 -->  
  21.     <!-- address为wsdl的访问地址 -->  
  22.     <jaxws:endpoint id="hello" implementor="#helloWorld" address="/hjy" >  
  23.         <!-- 添加了2个In拦截器,如果不添加拦截器可直接注释掉如下代码 -->  
  24.         <jaxws:inInterceptors>  
  25.             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>  
  26.             <bean class="com.tgb.auth.AuthInterceptor"/>  
  27.         </jaxws:inInterceptors>  
  28.         <!-- 如果要配置Out拦截器,使用outInterceptors -->  
  29.     </jaxws:endpoint>  
  30. </beans>                

以上配置已经完成,对于接口和相应的实现参考之前的博客即可,实现中对于new的内容使用spring管理起来

 

HelloWorldImpl:

[java] view plaincopyprint?
  1. package com.tgb.service.impl;  
  2.   
  3. import java.util.Date;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import javax.jws.WebService;  
  8.   
  9. import com.tgb.domain.Cat;  
  10. import com.tgb.domain.User;  
  11. import com.tgb.service.HelloWorld;  
  12. import com.tgb.service.UserService;  
  13.   
  14. @WebService(endpointInterface="com.tgb.service.HelloWorld",serviceName="HelloWorldImpl")  
  15. public class HelloWorldImpl implements HelloWorld{  
  16.   
  17.     private UserService userService;  
  18.       
  19.     public UserService getUserService() {  
  20.         return userService;  
  21.     }  
  22.   
  23.     public void setUserService(UserService userService) {  
  24.         this.userService = userService;  
  25.     }  
  26.   
  27.     public String sayHi(String name) {  
  28.               
  29.         return name+"您好!现在时间为:"+new Date();  
  30.     }  
  31.       
  32.     @Override  
  33.     public List<Cat> getCatsByUser(User user) {  
  34.         //在实际项目中,web service组件自己并不会去实现业务功能  
  35.         //它只是调用业务逻辑组件的方法来暴露web service  
  36. //      UserService us=new UserServiceImpl();  
  37.           
  38.         return userService.getCatsByUser(user);  
  39.     }  
  40.   
  41.     @Override  
  42.     public Map<String, Cat> getAllCats() {  
  43. //      UserService us=new UserServiceImpl();  
  44.         return userService.getAllCats();  
  45.     }  
  46.   
  47.       
  48. }  

启动tomcat服务器:

 

访问如下地址:http://192.168.24.215:8080/CXF_Spring_Server





新建客户端项目CXF_Spring_Client,生成客户端代码:




注意:

有些版本拷贝后,类中的super()会出错,要加上-frontendjaxws21,参看如上截图

 

客户端调用:


[java] view plaincopyprint?
  1. package com.tgb.client;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.cxf.endpoint.Client;  
  6. import org.apache.cxf.frontend.ClientProxy;  
  7.   
  8. import com.tgb.auth.AddHeaderInterceptor;  
  9. import com.tgb.service.Cat;  
  10. import com.tgb.service.HelloWorld;  
  11. import com.tgb.service.User;  
  12. import com.tgb.service.impl.HelloWorldImpl;  
  13.   
  14. public class client {  
  15.   
  16.     public static void main(String[] args){  
  17.         HelloWorldImpl factory=new HelloWorldImpl();  
  18.         //此处返回的只是远程Web Service的代理  
  19.         HelloWorld hw=factory.getHelloWorldImplPort();  
  20.           
  21.         /** 
  22.          * 添加的拦截器 
  23.          */  
  24.         Client client=ClientProxy.getClient(hw);  
  25.         //参数为输入的用户名,密码  
  26.         client.getOutInterceptors().add(new AddHeaderInterceptor("hejingyuan","hjy"));  
  27.           
  28.           
  29.         System.out.println(hw.sayHi("hejingyuan"));       
  30.         System.out.println("--------------------------");  
  31.           
  32.         User user=new User();  
  33.         user.setId(20);  
  34.         user.setName("孙悟空");  
  35.         user.setPass("111");  
  36.         user.setAddress("花果山");  
  37.           
  38.         List<Cat> cats=hw.getCatsByUser(user);  
  39.         for(Cat cat:cats){  
  40.             System.out.println(cat.getName());  
  41.         }  
  42.           
  43.         System.out.println("--------------------------");  
  44.           
  45.         System.out.println(hw.getAllCats().getEntry().get(0).getKey());  
  46.         System.out.println(hw.getAllCats().getEntry().get(0).getValue().getName());  
  47.           
  48.     }  
  49. }  



由于我们在服务端添加了拦截器,故客户端必须要添加相应的拦截器给服务端提供参数,否则客户端调用失败




另一个方面:调用远程的Web Service服务

 

新建客户端项目CXF_Spring_Web_Client,生成客户端代码




web.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.       
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  10.     </context-param>  
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.     </listener>  
  14.       
  15.     <filter>  
  16.         <filter-name>struts2</filter-name>  
  17.         <filter-class>  
  18.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  19.     </filter>  
  20.     <filter-mapping>  
  21.         <filter-name>struts2</filter-name>  
  22.         <url-pattern>/*</url-pattern>  
  23.     </filter-mapping>  
  24.           
  25. </web-app>  

applicationContext.xml:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"    
  5.     xsi:schemaLocation="    
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  8.       
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  12.       
  13.     <bean id="listCatsAction" class="com.tgb.action.ListCatsAction">  
  14.         <property name="helloWorld" ref="helloWorld"></property>  
  15.     </bean>  
  16.         
  17.     <!-- 配置远程webservice代理 -->  
  18.     <!-- id="helloWorld"对应 action setHelloWorld() -->  
  19.     <jaxws:client id="helloWorld"  
  20.         serviceClass="com.tgb.service.HelloWorld"  
  21.         address="http://localhost:8080/CXF_Spring_Server/hjy">  
  22.         <jaxws:outInterceptors>  
  23.         <!-- 配置输出拦截器  -->   
  24.             <bean class="com.tgb.auth.AddHeaderInterceptor" >  
  25.                 <constructor-arg value="hejingyuan"/>  
  26.                 <constructor-arg value="hjy"/>  
  27.             </bean>  
  28.         </jaxws:outInterceptors>  
  29.     </jaxws:client>  
  30.       
  31. </beans>  

Struts.xml


[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5.     <package name="default" namespace="/" extends="struts-default">  
  6.         <action name="listCats" class="com.tgb.action.ListCatsAction" >  
  7.             <result name="SUCCESS">/WEB-INF/page/listcats.jsp</result>  
  8.         </action>  
  9.     </package>  
  10. </struts>      

调用代码:

[java] view plaincopyprint?
  1. package com.tgb.action;  
  2.   
  3. import org.apache.cxf.endpoint.Client;  
  4. import org.apache.cxf.frontend.ClientProxy;  
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.tgb.auth.AddHeaderInterceptor;  
  8. import com.tgb.service.HelloWorld;  
  9.   
  10.   
  11. public class ListCatsAction extends ActionSupport{  
  12.   
  13.       
  14.     private HelloWorld helloWorld;  
  15.           
  16.     public HelloWorld getHelloWorld() {  
  17.         return helloWorld;  
  18.     }  
  19.   
  20.     public void setHelloWorld(HelloWorld helloWorld) {  
  21.         this.helloWorld = helloWorld;  
  22.     }  
  23.   
  24.     @Override  
  25.     public String execute() throws Exception {  
  26.         /** 
  27.          * 添加的拦截器 
  28.          */  
  29.         Client client=ClientProxy.getClient(helloWorld);  
  30.         //参数为输入的用户名,密码  
  31.         client.getOutInterceptors().add(new AddHeaderInterceptor("hejingyuan","hjy"));  
  32.         helloWorld.sayHi("hejingyuan");               
  33.         return "SUCCESS";  
  34.     }  
  35.       
  36. }  

最终的项目结构:




总结:


     以上与Spring的整合,概括的说,在应用Spring的框架过程中,一种是我们如何对外提供服务,另一种是我们如何获取已经提供好的服务。在整个过程中,只有几个关键点,很简单,但是要注意jar包的版本问题。


转载地址:http://blog.csdn.net/hejingyuan6/article/details/47152237




0 0