Develop Web Service (axis) in myeclipse)

Source: Internet
Author: User
Tags wsdl
Description and conventions:

This article describes how to use APIs provided by axis, an open-source Apache project, to implement web services. The examples mentioned in this article are only proposed to facilitate the description in this article. Please forgive me for any exceptions.

Readers of this article should have the basis for Java Web application development. It should have basic specifications such as WSDL, soap, and XML. Familiar with eclipse + myeclipse development environment.

This article can be reproduced at will, but the author's signature must be kept.

I. Environment preparation

To use axis to develop Web Services, you must prepare the Web server and the axis API. The Web container used in this article is tomcat5.5, and the axis API uses version 2.

1.1Software Download preparation

Tomcat: http://tomcat.apache.org/download-55.cgi#5.5.20

Axis standard package:

Http://ftp.wayne.edu/apache/ws/axis2/1_0/axis2-std-1.0-bin.zip

Axis war package:

Http://ftp.wayne.edu/apache/ws/axis2/1_0/axis2.war

Axis Eclipse plug-in (Code Generation and packaging tools ):

Http://apache.justdn.org/ws/axis2/tools/1_0/Axis2_Code_Generator.zip

Http://apache.justdn.org/ws/axis2/tools/1_0/Axis2_Service_Archiver.zip

Eclipse + myeclipse: can be downloaded from the official website (this article is 3.2 + 5.0ga)

1.2 Installation

A. First, build a development environment and decompress the downloaded eclipse file to a directory.

B. Extract the two plug-in files downloaded to axis to the plug-in subdirectory under the eclipse installation directory.

C. Install myeclipse5.0ga. Start myeclipse and select "file-> New-> Other" to find the following wizards. These are very important tools used in this article.

 

D. Set up the web services deployment environment. Download

Tomcat decompress the package to a directory. Complete web iner

.

E. Copy the axis2.war package to webapps under the tomcat installation directory.

Directory.

F. start Tomcat (startup in tomca_home/bin in Windows. BAT; in Linux, the Unix environment is startup. sh file), open the browser and enter: http: // ip: Port/axis2 to view. (If the address of the accompanying child file is not modified, it should be http: // localhost: 8080/axis.) If the following page is displayed, the installation is complete.

 

 

 

Ii. Quick Start

After the environment is ready, start with a simple example. This gives you an understanding of the general process of using axis to develop Web Services. This example is sayhello. the requester inputs a name string and returns a greeting.

For example, if Tom is input, the returned result is hi, Tom. How are you?

2.1WriteWSDL

A. Start myeclipse, create a new webapps (file-> New-> Project-> Web project), and set the project name to sayhello. The other settings remain unchanged.

B. Select File-> New-other menu, go to myeclipse-> Web Services, and select WSDL.

C. Select the "src" directory as the "enter or select the parent folder" value, and set the "file name" value to sayhello. WSDL. Click "Next" to go to the next step.

 

 

D. Set the target namespace to "namespaces. These values can be set as needed.

 

E. Click "finish" to view the interface of the WSDL designer provided by myeclipse. You can see that the designer adds a current operation for the WSDL by default.

That is, "newoperation" in the figure ". Change it to sayhello, for example:

 

Click the "Source" tab on the design page to view the code. As shown below: we can see that we have defined a sayhello Web services. She provides a sayhello method, and she can accept a string (in fact, it is a TNS: sayhellorequest object, she encapsulated the string) type input parameter sayhellorequest and returned a string (actually a tns: sayhelloresponse object, which encapsulates the string) type sayhelloresponse result. For more information about WSDL, see W3C standard documentation.

 

<? XML version = "1.0" encoding = "UTF-8"?>

<WSDL: Definitions xmlns: Soap = "The http://schemas.xmlsoap.org/wsdl/soap/" xmlns: TNS = "The http://ws.tonyzhangcn.org/SayHello/" xmlns: WSDL = "The http://schemas.xmlsoap.org/wsdl/" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" name = "sayhello" targetnamespace = "http://ws.tonyzhangcn.org/SayHello/">

<WSDL: types>

<XSD: schema targetnamespace = "http://ws.tonyzhangcn.org/SayHello/">

<XSD: element name = "sayhelloresponse" type = "XSD: string"/>

<XSD: element name = "sayhellorequest" type = "XSD: string"/>

</XSD: schema>

</WSDL: types>

<WSDL: Message name = "sayhelloresponse">

<WSDL: Part element = "TNS: sayhelloresponse" name = "sayhelloresponse"/>

</WSDL: Message>

<WSDL: Message name = "sayhellorequest">

<WSDL: Part element = "TNS: sayhellorequest" name = "sayhellorequest"/>

</WSDL: Message>

<WSDL: porttype name = "sayhello">

<WSDL: Operation name = "sayhello">

<WSDL: input message = "TNS: sayhellorequest"/>

<WSDL: Output Message = "TNS: sayhelloresponse"/>

</WSDL: Operation>

</WSDL: porttype>

<WSDL: Binding name = "sayhellosoap" type = "TNS: sayhello">

<Soap: Binding style = "document" Transport = "http://schemas.xmlsoap.org/soap/http"/>

<WSDL: Operation name = "sayhello">

<Soap: Operation soapaction = "http://ws.tonyzhangcn.org/SayHello/NewOperation"/>

<WSDL: input>

<Soap: Body use = "literal"/>

</WSDL: input>

<WSDL: output>

<Soap: Body use = "literal"/>

</WSDL: output>

</WSDL: Operation>

</WSDL: Binding>

<WSDL: Service name = "sayhello">

<WSDL: Port binding = "TNS: sayhellosoap" name = "sayhellosoap">

<Soap: address location = "http://www.example.org/"/>

</WSDL: Port>

</WSDL: Service>

</WSDL: Definitions>

So far, the WSDL has been compiled.

2.2SlaveWSDLGenerateJavaCode

A. select "file-> New-> Other" from the menu, and select "axis2 code generator" under "axis2 wizards" in the dialog box ". click "Next" to go to the next page, keep "generate Java source code from WSDL file" selected, and click "Next" to go to the next step.

B. Click "Browse" to select the sayhello. WSDL file stored in the src directory and "Next" to go to the next step. The settings are as follows:

C. Click "Next" to go to the next page and set "output path" to the src directory of the project. Click "finish" and press F5 on the sayhello project to refresh the code. However, we can see a bunch of red "X" because the jar package of axis2 is not put into the class path and the package of the source code is not caused by org. tonyzhangcn. ws. sayhello.

 

To solve this problem, first select the "sayhello" project and right-click and select "build path-> Add External Archives .. ", select all jar packages in the axis2 web application WEB-INF/lib directory deployed above in Tomcat. Right-click the "src.org. tonyzhangcn. ws. sayhello" project, select "refactor-> rename", make the following changes, and select "OK.

 

At this time, we found that sayhellotest. Java under test.org. tonyzhangcn. ws. sayhello still has problems. Click the red "X" in front of the key child of the package in this file. Two options are displayed. Select the one below.

 

Select the red "X" before the public class sayhellotest, and select the first item.

 

After these changes, the generated code will be fine. Where

Sayhellorequest and sayhelloresponse are input and output parameter objects.

Sayhelloskeleton is a server object used to write business logic calls.

Sayhellostub is the class used by the client to locate the endpoint (that is, the address of the published Web Services). The client locates the web service and initiates a call according to the method provided by the client.

Sayhellomessagereceiverinout is a kind of Web Service Information Conversion Processing class for Synchronous calls.

The services. xml file under the resources directory is the description file of WebServices.

Sayhellotest is a client's testcase example. You can compile your own client call class based on this example.

2.3Write Business Code

Open the sayhelloskeleton. Java file and set the implementation method

 

PublicOrg. tonyzhangcn. ws. sayhello. sayhelloresponse sayhello (Org. tonyzhangcn. ws. sayhello. sayhellorequest param0)

{

// Todo fill this with the necessary business logic

Throw NewJava. Lang. unsupportedoperationexception ();

}

 

Change

PublicOrg. tonyzhangcn. ws. sayhello. sayhelloresponse sayhello (Org. tonyzhangcn. ws. sayhello. sayhellorequest request)

{

// Todo fill this with the necessary business logic

Try

{

Sayhelloresponse response = new sayhelloresponse ();

Response. setsayhelloresponse ("hi," + request. getsayhellorequest () + ". How are you? ");

ReturnResponse;

}Catch(Unsupportedoperationexception E)

{

ThrowE;

}

}

2.4Package

A. Select "file-> New-> Other" from the menu, and select "axis2 services Archive" under "axis2 wizards" in the dialog box. Click "Next" to go to the next page.

B. Select the directory where the compiled class file is located (this project should be the WEB-INF/classes under webroot) and "Next" to go to the next step.

C. Select the directory where the WSDL file is located. SRC is used here.

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.