WebService之XFire+Spring集成(使用注解)

  本文只是介绍XFire+Spring使用注解的集成,版本为Xfire 1.2.6+Spring 3.1.1,测试环境为Tomcat6.0。Xfire 1.2.6下载

        1.首先建一个Web工程,引入相应的jar包,Xfire开发最精简jar包下载

服务器端:

commons-logging-1.1.1.jar
jdom-1.0.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar

客户端:

com.springsource.org.junit-4.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.1.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.1.jar

        2.修改web.xml,加入以下代码:

[html]  view plain  copy
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>classpath:applicationContext.xml</param-value>  
  4. </context-param>  
  5.   
  6. <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8. </listener>  
  9.   
  10. <servlet>  
  11.     <servlet-name>xfireServlet</servlet-name>  
  12.     <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>  
  13. </servlet>  
  14. <servlet-mapping>  
  15.     <servlet-name>xfireServlet</servlet-name>  
  16.     <url-pattern>/service/*</url-pattern>  
  17. </servlet-mapping>  
        3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:

[html]  view plain  copy
  1. <context:component-scan base-package="my.webservice" />  
  2.   
  3. <!-- XFire start -->  
  4. <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />  
  5. <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />  
  6. <bean id="jsr181HandlerMapping"  class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">  
  7.     <property name="xfire" ref="xfire" />  
  8.     <property name="webAnnotations" ref="webAnnotations" />  
  9. </bean>  
  10. <!-- XFire end -->  
        4.定义WebService接口,添加相应注解:

[java]  view plain  copy
  1. @WebService  
  2. public interface IBookService {  
  3.   
  4.     @WebMethod  
  5.     public Book getBook();  
  6. }  
        5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:

[java]  view plain  copy
  1. @Component  
  2. @WebService(serviceName="BookService",  
  3.         endpointInterface = "my.webservice.IBookService")  
  4. public class BookServiceImpl implements IBookService {  
  5.   
  6.     @Override  
  7.     public Book getBook() {  
  8.           
  9.         Book b = new Book(1"Java核心思想"100);  
  10.           
  11.         System.out.println(">>>>>>Server: " + b);  
  12.           
  13.         return b;  
  14.     }  
  15. }  

        6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。

        7.客户端调用服务端方法:

        (1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:

wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl
-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。
wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。

[java]  view plain  copy
  1. @Test  
  2. public void testBookService() {  
  3.     Service serviceModel = new ObjectServiceFactory().create(IBookService.class);  
  4.     String url = "http://127.0.0.1:8080/XFireTest/service/BookService";  
  5.     IBookService service = null;  
  6.       
  7.     try {  
  8.         service = (IBookService) new XFireProxyFactory().create(serviceModel, url);  
  9.         Book b = service.getBook();  
  10.         System.out.println(">>>>>>>>Client: " + b);  
  11.     } catch (Exception e) {  
  12.         e.printStackTrace();  
  13.     }  
  14. }  

        (2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。

[java]  view plain  copy
  1. @Test  
  2. public void wsdlTest() throws MalformedURLException, Exception  {  
  3.     Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl"));  
  4.     Object[] results = client.invoke("getBook"new Object[] {});  
  5.     System.out.println(results[0]);   
  6. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值