本Demo为实现最基本的WebService的服务器端与客户端的互动,基于XFire
XFire是新一代的JavaWeb服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。
服务器端代码大致如下
package com.gdie.xfire;
public class TestService implements ITestService {
public int add(int p1, int p2) {
// TODO Auto-generated method stub
return p1 + p2;
}
}
服务器端配置文件如下
service.xml
TestService
http://com/gdie/xfire/ITestService
com.gdie.xfire.ITestService
com.gdie.xfire.TestService
web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp
XFireServlet
org.codehaus.xfire.transport.http.XFireConfigurableServlet
0
XFireServlet
/services/*
客户端代码大致如下
package com.gdie.xfire.test;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import com.gdie.xfire.ITestService;;
public class MyClient {
public static void main(String[] args) {
try{
Service serviceModel = new ObjectServiceFactory().create(
ITestService.class,"TestService","http://com/gdie/xfire/ITestService",null);
ITestService service = (ITestService) new XFireProxyFactory().create(
serviceModel,"http://localhost:8080/TestXFire/services/TestService");
System.out.println("返回值是:"+service.add(5, 8));
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
客户端不需要更改配置文件
源代码有以下下载地址
XFire Jar文件
版权声明:本文为博主原创文章,未经博主允许不得转载。