`

Java使用XFire调用WebService接口 【转】

阅读更多
【摘自】http://www.cnblogs.com/simle/archive/2011/10/31/2230091.html

看了一些Java调用WebService接口的例子,很多都是Ctrl+C,Ctrl+V的,其中有很多拿来使用后发现有错误,令人郁闷,特此写了一篇经过测试的,只是一个小例子。

服务端(为客户端提供Webservice接口):

  使用工具:myeclipse-8.6.1-win32,apache-tomcat-7.0.11

  开发步骤:1.创建工程

       File->New->Web Service Project,弹出Web Service Project窗口,需要填写Project Name(例子是Demo),选择XFire,然后一路next,直到完成。

       创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

       2.创建WebService服务

        选择Toolbars上的New Web Service,弹出New Web Service窗口,选择Strategy:Create web service from Java class(Bottom-up scenario)并勾选下面的Create new Java bean,然后Next>,在Web service name中填写MyService,在Java package栏点击New...弹出窗口中Name:中填com.demo.service,然后点Finish。

        完成后,生成了一个Service的配置services.xml
View Code
复制代码

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://xfire.codehaus.org/config/1.0">
3
4     <service>
5         <name>IMyService</name>
6         <serviceClass>com.demo.service.IIMyService</serviceClass>
7         <implementationClass>
8             com.demo.service.IMyServiceImpl
9         </implementationClass>
10         <style>wrapped</style>
11         <use>literal</use>
12         <scope>application</scope>
13     </service></beans>

复制代码

        生成了接口和默认实现:
View Code
复制代码

1 package com.demo.service;
2 //Generated by MyEclipse
3
4 public interface IIMyService {
5    
6     public String example(String message);
7    
8 }

复制代码


View Code
复制代码

1 package com.demo.service;
2 //Generated by MyEclipse
3
4 public class IMyServiceImpl implements IIMyService {
5    
6     public String example(String message) {
7         return message;
8     }
9    
10 }

复制代码

  服务端代码生成完毕。
  测试服务端:1.前提:配置Tomcat服务器,并完成WebService服务端的部署,然后启动Tomcat。

        2.选择Toolbars上的Launch SOAP Web Service Explorer,Web Services Explorer窗口右侧WSDL Page,输入网址:http://localhost:8080/Demo/services/MyService?wsdl,显示如下图

        3.双击examlpe,输入hello,下面会显示out(string):hello,测试通过。

客户端(调用服务端提供的WebService接口方法):

  使用工具:eclipse

  需要引入如下包:commons-codec-1.2.jar、commons-httpclient-3.0.-rc2.jar、jdom.jar、xfire-all-1.2.6.jar、wsdl4j-1.5.1.jar、commons-logging-1.0.4.jar。

  开发步骤:1.创建工程

       File->New->Java Project->Project name:Demo,一路Next>,最后Finish,然后新建包com.demo.client,包中建立2个文件,一个是服务端接口文件(直接复制粘贴过来)IMyService.java,一个是测试文件Test.java,其代码如下:
View Code
复制代码

1 package com.demo.client;
2 import org.codehaus.xfire.client.XFireProxyFactory;
3 import org.codehaus.xfire.service.Service;
4 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
5
6 public class Test {
7     /**
8      * @param args
9 */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         String serviceUrl = "http://localhost:8080/Demo/services/MyService";
13         Service serviceModel = new ObjectServiceFactory().create(IMyService.class, null, "http://localhost:8080/Demo/services/MyService?wsdl", null);
14         XFireProxyFactory serviceFactory = new XFireProxyFactory();
15         try{
16             IMyService service = (IMyService)serviceFactory.create(serviceModel,serviceUrl);
17             String hello = service.example("hello");
18             System.out.println(hello);
19         }catch(Exception e){
20             e.printStackTrace();
21         }
22     }
23 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics