Java WebService Demo,基于XFire
本Demo为实现最基本的WebService的服务器端与客户端的互动,基于XFire
XFire是新一代的Java Web服务引擎,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
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>TestService</name> <namespace>http://com/gdie/xfire/ITestService</namespace> <serviceClass>com.gdie.xfire.ITestService</serviceClass> <implementationClass>com.gdie.xfire.TestService</implementationClass> </service> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
客户端代码大致如下
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(); } } }
客户端不需要更改配置文件
源代码有以下下载地址
版权声明:本文为博主原创文章,未经博主允许不得转载。