JAVA中使用XFire调用WebService接口

http://blog.csdn.net/wangjian223344/article/details/11899187

首先以helloworld为例。网上看了好多xifre的例子几乎没好用的烦燥

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

开发步骤:1.创建工程

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

                    创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  3.   <servlet>  
  4.     <servlet-name>XFireServlet</servlet-name>  
  5.     <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  
  6.     <load-on-startup>0</load-on-startup>  
  7.   </servlet>  
  8.   <servlet-mapping>  
  9.     <servlet-name>XFireServlet</servlet-name>  
  10.     <url-pattern>/services/*</url-pattern>  
  11.   </servlet-mapping>  
  12.   <welcome-file-list>  
  13.     <welcome-file>index.jsp</welcome-file>  
  14.   </welcome-file-list>  
  15. </web-app>  
2.创建WebService服务 配置services.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  <beans xmlns="http://xfire.codehaus.org/config/1.0">  
  3.      <service>  
  4.          <name>IMyService</name>  
  5.          <namespace>com.demo.service</namespace>  
  6.          <serviceClass>com.demo.service.IIMyService</serviceClass>  
  7.          <implementationClass>com.demo.service.IMyServiceImpl</implementationClass>  
  8.          <style>wrapped</style>  
  9.          <use>literal</use>  
  10.          <scope>application</scope>  
  11.      </service>  
  12. </beans>  
3.生成了接口和默认实现

[java]  view plain copy
  1. <pre name="code" class="java">IIMyService.java</pre>package com.demo.service;public interface IIMyService {public String example(String message);}  
[java]  view plain copy
  1. <pre name="code" class="java">IMyServiceImpl.java</pre>package com.demo.service;public class IMyServiceImpl implements IIMyService {public String example(String message) {// TODO 写自己的逻辑return message;}}  
服务端代码生成完毕。测试

http://localhost:9999/WebServiceProject/services/IMyService?wsdl 成功后

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

1.我们将创建HelloWorld项目中的类。如果你还没有这样做的话,你需要添加的XFireHTTP客户端库,该项目将包含您的Java测试客户端的构建路径

(1)在包装中,探索查看右键单击HelloWorld项目,并选择Build Path>从上下文菜单中添加库
(2)选择MyEclipse的图书馆
(3)选择的XFire HTTP客户端库

2.HelloWorldClient类

[html]  view plain copy
  1. package com.myeclipse.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4.   
  5. import org.codehaus.xfire.XFireFactory;  
  6. import org.codehaus.xfire.client.XFireProxyFactory;  
  7. import org.codehaus.xfire.service.Service;  
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  9.   
  10. import java.net.MalformedURLException;  
  11. import java.net.URL;  
  12.   
  13. import org.codehaus.xfire.client.Client;  
  14. import com.demo.client.IIMyService;  
  15.   
  16. public class HelloWorldClient {  
  17.   
  18.     public static void main(String[] args)throws MalformedURLException,  
  19.     Exception  {   
  20. //      Service srvcModel = new   ObjectServiceFactory().create(IIMyService.class);    
  21. //      XFireProxyFactory factory =     
  22. //         new XFireProxyFactory(XFireFactory.newInstance().getXFire());    
  23. //      String helloWorldURL =    
  24. //         "http://localhost:9999/WebServiceProject/services/MyService";    
  25. //      try {    
  26. //          IIMyService srvc = (IIMyService)factory.create( srvcModel, helloWorldURL);    
  27. //         String result = srvc.example("hello world");    
  28. //         System.out.print(result);    
  29. //      } catch (MalformedURLException e) {    
  30. //         e.printStackTrace();    
  31. //      }   
  32.         Client client = new Client(new URL(  
  33.                 "http://localhost:9999/WebServiceProject/services/IMyService?wsdl"));  
  34.         Object[] results = client  
  35.                 .invoke("example", new Object[] { "hello world" });  
  36.         System.out.println(results[0]);  
  37.           
  38.       }   
  39.   
  40. }  
3成功:)


注:http://www.myeclipseide.com/documentation/quickstarts/webservices/



java调用net开发的webservice实例下载 问题终于解决了,之前加上?wsdl的时候报如下错: - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect - Retrying request - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect - Retrying request - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect - Retrying request - java.net.ConnectException: Connection timed out: connect org.codehaus.xfire.fault.XFireFault: Couldn't send message. at org.codehaus.xfire.fault.XFireFault.createFault(XFireFault.java:89) at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:30) at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131) at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79) at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114) at org.codehaus.xfire.client.Client.invoke(Client.java:336) at org.codehaus.xfire.client.Client.invoke(Client.java:368) at cn.watersoft.reportinfo.test.test.main(test.java:22) Caused by: org.codehaus.xfire.XFireException: Couldn't send message. at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:145) at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48) at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26) ... 6 more Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at java.net.Socket.connect(Socket.java:469) at java.net.Socket.(Socket.java:366) at java.net.Socket.(Socket.java:240) at org.apach
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值