首先以helloworld为例。网上看了好多xifre的例子几乎没好用的烦燥
一、服务端(为客户端提供Webservice接口)
开发步骤:1.创建工程
File->New->Web Service Project,弹出Web Service Project窗口,需要填写ProjectName(WebServiceProject),选择XFire,然后一路next,直到完成。
创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。
<?xml version="1.0" encoding="UTF-8"?> <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"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>2.创建WebService服务 配置services.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>IMyService</name> <namespace>com.demo.service</namespace> <serviceClass>com.demo.service.IIMyService</serviceClass> <implementationClass>com.demo.service.IMyServiceImpl</implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service> </beans>3.生成了接口和默认实现
IIMyService.javapackage com.demo.service;public interface IIMyService {public String example(String message);}
服务端代码生成完毕。测试IMyServiceImpl.javapackage 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类
package com.myeclipse.client; import java.net.MalformedURLException; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import java.net.MalformedURLException; import java.net.URL; import org.codehaus.xfire.client.Client; import com.demo.client.IIMyService; public class HelloWorldClient { public static void main(String[] args)throws MalformedURLException, Exception { // Service srvcModel = new ObjectServiceFactory().create(IIMyService.class); // XFireProxyFactory factory = // new XFireProxyFactory(XFireFactory.newInstance().getXFire()); // String helloWorldURL = // "http://localhost:9999/WebServiceProject/services/MyService"; // try { // IIMyService srvc = (IIMyService)factory.create( srvcModel, helloWorldURL); // String result = srvc.example("hello world"); // System.out.print(result); // } catch (MalformedURLException e) { // e.printStackTrace(); // } Client client = new Client(new URL( "http://localhost:9999/WebServiceProject/services/IMyService?wsdl")); Object[] results = client .invoke("example", new Object[] { "hello world" }); System.out.println(results[0]); } }3成功:)
注:http://www.myeclipseide.com/documentation/quickstarts/webservices/