使用Xfire和Spring配置web service

来源:互联网 发布:cos假毛淘宝店铺推荐 编辑:程序博客网 时间:2024/06/05 18:46
 

一.环境

Jdk1.5,Eclipse3.2,MyEclipse5.5,Xfire1.2.6,Spring1.2

二.服务端

使用Xfire配合spring把一个pojo发布成web服务有很多种方法,这里采用的是配置最简单的JSR181注解。

1.       建立web工程,工程名为wsserver

2.       把spring的核心库(Spring1.2 Core Libraries)导入工程

3.       把xfire包导入工程,在Eclipse里右击工程,MyEclipse->Add Web Service Cabalities->在servlet class里选择org.codehaus.xfire.spring.XfireSpringServlet->在servlet mapping里把名字改为/webservices/*(避免和OpenSessionInViewFilter冲突)->finish,在正确配置这两步后,在web.xml做如下配置

Xml代码 <?xml version="1.0" encoding="UTF-8"?>  
  1. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  2.   <context-param>  
  3.         <param-name>contextConfigLocation</param-name>  
  4.         <param-value>  
  5.             /WEB-INF/applicationContext.xml   
  6.         </param-value>  
  7.     </context-param>  
  8.     <listener>  
  9.         <listener-class>  
  10.             org.springframework.web.context.ContextLoaderListener   
  11.         </listener-class>  
  12.     </listener>  
  13.     <listener>  
  14.         <listener-class>  
  15.             org.springframework.web.util.IntrospectorCleanupListener   
  16.         </listener-class>  
  17.     </listener>  
  18.   
  19.     <!-- begin XFire 配置 -->  
  20.     <servlet>  
  21.         <!-- 配合Spring容器中XFire一起工作的Servlet-->  
  22.         <servlet-name>xfireServlet</servlet-name>  
  23.         <servlet-class>  
  24.             org.codehaus.xfire.spring.XFireSpringServlet   
  25.         </servlet-class>  
  26.     </servlet>  
  27.     <servlet-mapping>  
  28.         <servlet-name>xfireServlet</servlet-name>  
  29.         <!-- 在这个URI下开放Web Service服务 -->  
  30.         <url-pattern>/webservices/*</url-pattern>  
  31.     </servlet-mapping>  
  32.     <!-- end XFire 配置 -->  
  33. </web-app>  
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 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>/WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- begin XFire 配置 --><servlet><!-- 配合Spring容器中XFire一起工作的Servlet--><servlet-name>xfireServlet</servlet-name><servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class></servlet><servlet-mapping><servlet-name>xfireServlet</servlet-name><!-- 在这个URI下开放Web Service服务 --><url-pattern>/webservices/*</url-pattern></servlet-mapping><!-- end XFire 配置 --></web-app>

 

 

 

 4.    编写接口类IHelloWS,并在类上做@WebService标注

  

Java代码 复制代码 收藏代码
  1. package test;   
  2.   
  3. import java.util.List;   
  4.   
  5. import javax.jws.WebService;   
  6.   
  7. @WebService  
  8. public interface IHelloWS {   
  9.        
  10.     public void sayHello();   
  11.        
  12.     public String whatSay();   
  13.        
  14.     public void sayHello(Foo foo);   
  15.        
  16.     public List<Foo> says(List<Boo> list);   
  17.        
  18. }  
package test;import java.util.List;import javax.jws.WebService;@WebServicepublic interface IHelloWS {public void sayHello();public String whatSay();public void sayHello(Foo foo);public List<Foo> says(List<Boo> list);}

 

以上需要注意的是web服务中的方法的输入/出参数中若有使用到集合对象,则要使用泛型。否则需要做aegis配置。

 

5.       编写IHelloWS的实现类HelloWSImpl,并在类上做@WebService(serviceName="helloUT",endpointInterface="test.IHelloWS")标注,其中serviceName是这这个服务的名称,默认为接口实现类的名称,endpointInterface是该类实现的接口的类全名。

 

Java代码 复制代码 收藏代码
  1. package test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import javax.jws.WebService;   
  7.   
  8. @WebService(serviceName="helloUT",endpointInterface="test.IHelloWS")   
  9. public class HelloWSImpl implements IHelloWS {   
  10.        
  11.     public void sayHello() {   
  12.         System.out.println("hello ws!");   
  13.     }   
  14.   
  15.     public String whatSay() {   
  16.         return "hello ws!";   
  17.     }   
  18.   
  19.     public void sayHello(Foo foo) {   
  20.         System.out.println("hello "+ foo.getName());   
  21.     }   
  22.   
  23.     public List<Foo> says(List<Boo> list) {   
  24.         List<Foo> lists = new ArrayList<Foo>();    
  25.         for (int i = 0;i<list.size();i++) {   
  26.             Foo foo = new Foo();   
  27.             foo.setName(list.get(i).getFirstName()+list.get(i).getLastName());   
  28.             lists.add(foo);   
  29.         }   
  30.         return lists;   
  31.     }   
  32.   
  33. }  
package test;import java.util.ArrayList;import java.util.List;import javax.jws.WebService;@WebService(serviceName="helloUT",endpointInterface="test.IHelloWS")public class HelloWSImpl implements IHelloWS {public void sayHello() {System.out.println("hello ws!");}public String whatSay() {return "hello ws!";}public void sayHello(Foo foo) {System.out.println("hello "+ foo.getName());}public List<Foo> says(List<Boo> list) {List<Foo> lists = new ArrayList<Foo>(); for (int i = 0;i<list.size();i++) {Foo foo = new Foo();foo.setName(list.get(i).getFirstName()+list.get(i).getLastName());lists.add(foo);}return lists;}}

 

 

6.    以上接口和实现类用到的两个类Foo和Boo如下

 

Java代码 复制代码 收藏代码
  1. package test;   
  2.   
  3. public class Foo {   
  4.   
  5.     public String name;   
  6.   
  7.     public String getName() {   
  8.         return name;   
  9.     }   
  10.   
  11.     public void setName(String name) {   
  12.         this.name = name;   
  13.     }   
  14. }   
  15.   
  16.   
  17. package test;   
  18.   
  19. public class Boo {   
  20.     public String firstName;   
  21.   
  22.     public String lastName;   
  23.   
  24.     public String getFirstName() {   
  25.         return firstName;   
  26.     }   
  27.   
  28.     public void setFirstName(String firstName) {   
  29.         this.firstName = firstName;   
  30.     }   
  31.   
  32.     public String getLastName() {   
  33.         return lastName;   
  34.     }   
  35.   
  36.     public void setLastName(String lastName) {   
  37.         this.lastName = lastName;   
  38.     }   
  39.   
  40. }  
package test;public class Foo {public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}package test;public class Boo {public String firstName;public String lastName;public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}}

 

7.    在ApplicationContext中做如下配置

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3.   
  4. <beans>  
  5.        
  6.     <!-- 要发布成web服务的pojo -->  
  7.     <bean id="serviceBean" class="test.HelloWSImpl"/>  
  8.        
  9.     <!-- 引入XFire预配置信息 -->  
  10.     <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />  
  11.        
  12.     <!-- 获得applicationContext中所有bean的JSR181 annotation -->  
  13.     <bean id="webAnnotations"  
  14.         class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />  
  15.     <bean id="jsr181HandlerMapping"  
  16.         class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">  
  17.         <property name="xfire" ref="xfire" />  
  18.         <property name="webAnnotations" ref="webAnnotations" />  
  19.     </bean>    
  20. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><!-- 要发布成web服务的pojo --><bean id="serviceBean" class="test.HelloWSImpl"/><!-- 引入XFire预配置信息 --><import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /><!-- 获得applicationContext中所有bean的JSR181 annotation --><bean id="webAnnotations"class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /><bean id="jsr181HandlerMapping"class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"><property name="xfire" ref="xfire" /><property name="webAnnotations" ref="webAnnotations" /></bean> </beans>

  

8.    启动服务,在浏览器地址栏中输入http://localhost:8090/wsserver/webservice/helloUT?wsdl,注意这里的helloUT是web服务的名字,它是配在接口实现类的@WebService的标注的serviceName成员中的,若没有配置值则默认为接口实现类的名称(这里还有点要注意:若不使用JSR181的方式发布web服务则默认的名字是接口的名称)。若能看到正确的wsdl文件则表明服务端已大功告成了。

 

三.客户端

客户端调用web服务也有很多方法,这里采用的是获取服务端接口类再配合wsdl地址来访问威web服务。并且把所有的web服务以bean的形式配置在spring容器中,在实际的应用中就可以使用注入的方式来获取web服务。

1.       建立web工程,工程名为wsclient

2.       拷贝服务端的接口类和参数类(IHelloWS,Foo,Boo)到客户端,这里需要注意的是它们的包结构必须和服务端保持一致!

3.       把spring的核心库(Spring1.2 Core Libraries)导入工程

4.       把xfire包导入工程,在Eclipse里右击工程,MyEclipse->Add Web Service Cabalities->finish

5.       把commons-httpclient.jar包导入工程

6.       在ApplicationContext中配置服务bean

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3.   
  4. <beans>  
  5.   
  6.     <bean id="helloService"  
  7.         class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">  
  8.         <!-- web服务接口类 -->  
  9.         <property name="serviceClass" value="test.IHelloWS" />  
  10.         <!-- web服务wsdl地址 -->  
  11.         <property name="wsdlDocumentUrl"  
  12.             value="http://localhost:8090/wsserver/webservice/helloUT?wsdl" />  
  13.     </bean>  
  14.   
  15. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean id="helloService"class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean"><!-- web服务接口类 --><property name="serviceClass" value="test.IHelloWS" /><!-- web服务wsdl地址 --><property name="wsdlDocumentUrl"value="http://localhost:8090/wsserver/webservice/helloUT?wsdl" /></bean></beans>

 

 

 

 

7.       写个测试类HelloWSTest

Java代码 复制代码 收藏代码
  1. package test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.springframework.context.ApplicationContext;   
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  8.   
  9. public class HelloWSTest {   
  10.   
  11.     public static void main(String[] args) {   
  12.            
  13.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
  14.         IHelloWS service = (IHelloWS)ctx.getBean("helloService");   
  15.            
  16.         service.sayHello();   
  17.         System.out.println(service.whatSay());   
  18.            
  19.         Foo foo = new Foo();   
  20.         foo.setName("zhouQ");   
  21.         service.sayHello(foo);   
  22.            
  23.         List<Boo> list = new ArrayList<Boo>();   
  24.         Boo boo1 = new Boo();   
  25.         boo1.setFirstName("zhou");   
  26.         boo1.setLastName("Q");   
  27.         list.add(boo1);   
  28.         Boo boo2 = new Boo();   
  29.         boo2.setFirstName("YY");   
  30.         boo2.setLastName("ning");   
  31.         list.add(boo2);   
  32.            
  33.         List<Foo> lists = service.says(list);   
  34.         for (int i = 0;i<lists.size();i++) {   
  35.             foo = lists.get(i);   
  36.             System.out.println(foo.getName());   
  37.         }   
  38.     }   
  39.   
  40. }  
原创粉丝点击