Xfire development instance-helloworld

Source: Internet
Author: User

 

This article describes how to use xfire to develop Web service applications starting with the basic helloworld case. In the future, you can develop complex web service applications.

Development Environment: eclipse3.2 + tomcat5.5.9 + xfire1.2.6

I. Create a project xfireproject in eclipse

1. Create the source folder SRC. Main; create the helloservice. Java file, which declares only a simple sayhello (string name) method ,. The content of the Java file is as follows:

/**
*
*/
Package com. Liuxiang. xfire;

/**
* Simple example: helloworld
* Helloservice. Java
* COM. Liuxiang. xfire
* Xfireproject
* @ Author Liuxiang mailto: juxtapose@163.com
* 05:01:38 pm
*
*/
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 to declare a service. The content of the service. xml file is 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 service. xml file to Tomcat. The file location is as follows:

Webapps/xfire/WEB-INF/classes/META-INF/xfire/services. xml;

Webapps/xfire/WEB-INF/classes/COM/Liuxiang/xfire/helloservice. Class;

4. start Tomcat. After correctly starting tomcat, enter http: // localhost: 8080/xfire/services/In the IE address bar. The following page appears: this page shows the deployed helloservice normally. For example:

If it is displayed correctly, the deployment is successful. If it cannot be correctly displayed, refer to my previous article: xfire1.2.6 configuration.

Click [WSDL]. http: // localhost: 8080/xfire/services/helloservice? WSDL: A generated WSDL file.

5. Generate a web service client call File

Xfire provides two methods to generate client tests, one is ant script and the other is xfire Eclipse plug-in; this article describes how to generate a client using ant script.

Add a build. xml file to the xfireproject project. Xfire provides an ant task:

<Taskdef name = "wsgen" classname = "org. codehaus. xfire. gen. wsgentask" classpathref = "myclasspath"/>

The content of the build. xml file is 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>
<! -- Use the xfire ant task to generate the storage location of client code -->
<Property name = "code_path" value = "src. Client"/>
<! -- The WSDL file for generating client code -->
<Property name = "wsdl_path" value = "http: // localhost: 8080/xfire/services/helloservice? WSDL "/>
<! -- Generate the package name of the client code -->
<Property name = "code_package" value = "com. Liuxiang. xfire. Client"/>

<! -- Remove classes directory for clean build -->
<Target name = "clean" Description = "prepare for 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 attribute defines the folder where the code is created
WSDL is the Web Service's WSDL File
Package indicates the package of the created code.
-->
<Wsgen outputdirectory = "$ {code_path}" WSDL = "$ {wsdl_path}" package = "$ {code_package}" binding = "xmlbeans"/>
</Target>
</Project>

 

Execute the ant script and the client code will be generated. There are three files in total. Will be placed under the package com. Liuxiang. xfire. client. The files are:

Helloserviceclient. Java, helloserviceimpl. Java, helloserviceporttype. Java

6. Compile the test code. Compile the testclient. Java file by calling the code generated in 5. The file content is 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;

/***//**
* Call through the client generated by xfire
*
* Testclient. Java
* COM. Liuxiang. xfire
* Xfireproject
* @ Author Liuxiang mailto: juxtapose@163.com
* 06:54:36 pm
*
*/
Public class testclient ...{
/***//**
* Client Test
* Call a client generated by ANT script
*
* @ Param name refers to the input parameter, customer name
* @ Return returns the return value of sayhello ().
*/
Public static string testclient (string name )...{
Helloserviceclient hellosc = new helloserviceclient ();
Helloserviceporttype hellosp = hellosc. gethelloservicehttpport ();
String result = hellosp. sayhello (name );
Return result;
}

/***//**
* @ Param ARGs
* @ Throws exception
*/
Public static void main (string [] ARGs) throws exception ...{
System. Out. println (testclient ("Liuxiang "));
}

}

Run the code and enter the following information in the console:

Liuxiang, hello!

Indicates that the first web service use case has been successfully run. Later, you can develop the required web service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.