2007-09-11 20:09 Category: Web Service prev | Post Author Juxtapose | 1 Latest Reply | Trackback (0) | Permalink
This article starts with the most basic HelloWorld use case and describes how to develop a Web service application using Xfire. Complex Web Service applications can be developed in the future.
Development environment: eclipse3.2+tomcat5.5.9+xfire1.2.6
One: Create a new project in Eclipse Xfireproject
1. Create the source folder Src.main; Create a new Helloservice.java file that only declares a simple SayHello (String name) method. The contents of the Java file are as follows:/**
*
*/
Package com.liuxiang.xfire;
/**
* Simple Example HelloWorld example
* helloservice.java
* com.liuxiang.xfire
* xfireproject
* @author liuxiang mailto:juxtapose@163.com
* 2007-9-9 pm 05:01:38
*
*/
Public class helloservice {
public string sayhello (string name) {
return name + "Hello. " ;
}
}
2. Create a new Meta-inf/xfire/services.xml file in the Src.main directory, which is used to declare a service. The contents of the Service.xml file are as follows: <!--START Snippet:services--
< beans xmlns = "http://xfire.codehaus.org/config/1.0" >
< service >
< name > HelloService </name >
< namespace > Http://com.liuxiang.xfireDemo/HelloService </namespace >
< ServiceClass > Com.liuxiang.xfire.HelloService </serviceclass >
</Service >
</Beans >
<!--END Snippet:services--
3. Deploy the compiled Helloservice.class file and the Service.xml file to Tomcat. The location of the file is as follows:
Webappsxfireweb-infclassesmeta-infxfireservices.xml;
Webappsxfireweb-infclassescomliuxiangxfirehelloservice.class;
4. Start Tomcat; After you start Tomcat correctly, in the IE Address bar, enter: Http://localhost:8080/xfire/services/will appear the following page: This page shows the HelloService that you just deployed. The following figure:
If the image above is displayed correctly, the deployment was successful. If it doesn't display correctly, please refer to my previous article: xfire1.2.6 configuration.
Click [WSDL] and the HTTP://LOCALHOST:8080/XFIRE/SERVICES/HELLOSERVICE?WSDL will appear, which is the generated WSDL file.
5. Generate a Web service client call file
Xfire provides two ways to generate client-side tests, one that provides an ant script, and one that provides a Xfire eclipse plug-in; This article describes how to build a client by using an ant script.
First add a Build.xml file to the project Xfireproject. Xfire provides an ant task: < taskdef name = "Wsgen" classname = "org.codehaus.xfire.gen.WsGenTask" classpathref = "Myclasspath" />
The contents of the Build.xml file are as follows: <? XML version= "1.0"?>
< project name = "Xfireproject" default = "Genfiles" Basedir = "." >
< property name = "Lib" value = "Lib"/>
< path ID = "Myclasspath" >
< Fileset dir = "${lib}" >
< include name = "*.jar"/>
</fileset >
< pathelement location = "${genfiles}"/>
</path >
<!--generate client code with the Xfire ant task--
< property name = "Code_path" value = "Src.client"/>
<!--the WSDL file that needs to generate the client code--
< property name = "Wsdl_path" value = "http://localhost:8080/xfire/services/HelloService?wsdl"/>
<!--generate the package name for the client code--
< property name = "Code_package" value = "Com.liuxiang.xfire.client"/>
<!--Remove Classes directory for Clean build-
< target name = "clean" description = "Prepare to clean Build" >
< Delete dir = "${code_path}"/>
< mkdir dir = "${code_path}"/>
</Target >
<!--<target name= "genfiles" depends= "clean" description= "Generate the Files" >-
< target name = "Genfiles" description = "Generate The files" >
< taskdef name = "Wsgen" classname = "org.codehaus.xfire.gen.WsGenTask" classpathref = "Myclasspath"/>
<!--OutputDirectory property defines the folder where the code is created
WSDL is a WSDL file for a Web service
Package represents the code created by the package
-
< Wsgen outputdirectory = "${code_path}" WSDL = "${wsdl_path}" package = "${code_package}" binding = "XMLBeans"/>
</Target >
</Project >
Executing the ant script will generate the client code, a total of three files. will be placed under the package com.liuxiang.xfire.client, the files are:
Helloserviceclient.java, Helloserviceimpl.java, Helloserviceporttype.java
6. Write the test code, write the Testclient.java file by calling the code generated in 5. The contents of the file are as follows:/** *//**
*
*/
Package com.liuxiang.xfire;
Import java.net.MalformedURLException;
Import org.codehaus.xfire.xfire;
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 com.liuxiang.xfire.client.helloserviceclient;
Import Com.liuxiang.xfire.client.HelloServicePortType;
/** *//**
* calls through Xfire generated by the client
*
* testclient.java
* com.liuxiang.xfire
* xfireproject
* @author liuxiang mailto : juxtapose@163.com
* 2007-9-9 pm 06:54:36
*