Build a simple axis web service

Source: Internet
Author: User

1. Download the axis project on the official website (which will be useful in this case) and the source code, jar package, etc., which are:

Http://labs.renren.com/apache-mirror//ws/axis/1_4/

2. decompress the downloaded project or source code (either of the two can be). Decompress axis-bin-1.4 and you can see that the approximate directory is as follows:

 

Docs is a document, lib is a jar package, sample is an example, xmls is the xml required for the current project, webapps is the webroot directory of the current project;

Open the webapps directory and you will see an axis folder, which contains a WEB-INF folder and some pages to copy axis to your tomcat webapps directory. Start the tomcat service and access http: // localhost: 8080/axis/. If you see the following decoding, the deployment is successful:

 

In the future, we will not leave this project alone. It will play a major role in our axis1.x webService!

 

3. create our own web project. Here I create the AxisWebService. After creating the project, copy the jar package of lib in axis-bin that we just decompressed to the lib of the current project;

Axis-ant.jar

Axis. jar

Commons-discovery-0.2.jar

Commons-logging-1.0.4.jar

Jaxrpc. jar

Log4j-1.2.8.jar

Saaj. jar

Wsdl4j-1.5.1.jar

Activation-1.1.jar

Mail-1.4.jar

 

Create a webService file with the following code:

 

Package com. hoo. service;

/**
* <B> function: </B> axis WebService of jws
* @ Author hoojo
* @ CreateDate Dec 15,201 0 17:03:49
* @ File HelloWorldService. java
* @ Package com. hoo. service
* @ Project AxisWebService
* @ Blog http://blog.csdn.net/IBM_hoojo
* @ Email hoojo_@126.com
* @ Version 1.0
*/
Publicclass HelloWorldService {

Public String sayHello (String name, int age ){
Return name + "say: hello world! [Axis] my age is "+ age;
}
}

 

4. Copy HelloWorldService. java can be copied to the axis folder, that is, the axis under webapps under tomcat. Note: The important thing is to remove the package name from this java file, and rename this file as HelloWorldService. jws; if there is a package name, the class compiled after the request will be in the package path, so that we will not find the class in the current global jws, for details, you can go to the Project released under tomcat to see the jwsClass under the WEB-INF directory at a glance.

After the above work is completed, start the tomcat server and access http: // localhost: 8080/axis/HelloWorldService. jws

You will see:

There is a Web Service here

Click to see the WSDL

If you see the same thing, it proves that you have successfully deployed an axis1.x webService. Then we can click it to view the xml file of the wsdl. The content is as follows:

 

<? Xml version = "1.0" encoding = "UTF-8"?>
-<Wsdl: definitions targetNamespace = "http: // localhost: 8080/axis/HelloWorldService. jws "xmlns: apachesoap =" http://xml.apache.org/xml-soap "xmlns: impl =" http: // localhost: 8080/axis/HelloWorldService. jws "xmlns: intf =" http: // localhost: 8080/axis/HelloWorldService. jws "xmlns: soapenc =" http://schemas.xmlsoap.org/soap/encoding/ "xmlns: wsdl =" http://schemas.xmlsoap.org/wsdl/ "xmlns: wsdlsoap =" http://schemas.xmlsoap.org/wsdl/soap/ "xmlns: xsd =" http://www.w3.org/2001/XMLSchema ">
-<! --
WSDL created by Apache Axis version: 1.4
Built on Apr 22,200 6 (06:55:48 PDT)
-->
-<Wsdl: message name = "sayHelloResponse">
<Wsdl: part name = "sayHelloReturn" type = "xsd: string"/>
</Wsdl: message>
-<Wsdl: message name = "sayHelloRequest">
<Wsdl: part name = "name" type = "xsd: string"/>
<Wsdl: part name = "age" type = "xsd: int"/>
</Wsdl: message>
-<Wsdl: portType name = "HelloWorldService">
-<Wsdl: operation name = "sayHello" parameterOrder = "name age">
<Wsdl: input message = "impl: sayHelloRequest" name = "sayHelloRequest"/>
<Wsdl: output message = "impl: sayHelloResponse" name = "sayHelloResponse"/>
</Wsdl: operation>
</Wsdl: portType>
-<Wsdl: binding name = "HelloWorldServiceSoapBinding" type = "impl: HelloWorldService">
<Wsdlsoap: binding style = "rpc" transport = "http://schemas.xmlsoap.org/soap/http"/>
-<Wsdl: operation name = "sayHello">
<Wsdlsoap: operation soapAction = ""/>
-<Wsdl: input name = "sayHelloRequest">
<Wsdlsoap: body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http: // DefaultNamespace" use = "encoded"/>
</Wsdl: input>
-<Wsdl: output name = "sayHelloResponse">
<Wsdlsoap: body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http: // localhost: 8080/axis/HelloWorldService. jws" use = "encoded"/>
</Wsdl: output>
</Wsdl: operation>
</Wsdl: binding>
-<Wsdl: service name = "HelloWorldServiceService">
-<Wsdl: port binding = "impl: HelloWorldServiceSoapBinding" name = "HelloWorldService">
<Wsdlsoap: address location = "http: // localhost: 8080/axis/HelloWorldService. jws"/>

</Wsdl: port>
</Wsdl: service>
</Wsdl: definitions>

 

 

 

Analyze the content of the wsdl xml file:

TargetNamespace =Http: // localhost: 8080/axis/HelloWorldService. jws 

Is the webservice namespace we deployed, that is, the webService path we accessed.

<Wsdl: message name ="SayHelloResponse">

 <Wsdl: part name ="SayHelloReturn"Type ="Xsd: string"/>

 </Wsdl: message>

Is the information of the returned value,SayHelloResponseIndicates the response, that is, the return value. type indicates the type of the returned value.

 

<Wsdl: message name ="SayHelloRequest">

 <Wsdl: part name ="Name"Type ="Xsd: string"/>

 <Wsdl: part name ="Age"Type ="Xsd: int"/>

 </Wsdl: message>

Request method parameter information,SayHelloRequestThat is, the request. The part is the parameter, and the type is the parameter type.

 

<Wsdl: portType name ="HelloWorldService">

<Wsdl: operation name ="SayHello"ParameterOrder ="Name age">

<Wsdl: input message ="Impl: sayHelloRequest"Name ="SayHelloRequest"/>

<Wsdl: output message ="Impl: sayHelloResponse"Name ="SayHelloResponse"/>

</Wsdl: operation>

</Wsdl: portType>

PortType name is the name of the current webService, and operation is an operation that can be called. Name is the method name, parameterOrder is the parameter, input is the input parameter, output is the returned value;

 

<Wsdl: service name ="HelloWorldServiceService">

<Wsdl: port binding ="Impl: HelloWorldServiceSoapBinding"Name ="HelloWorldService">

<Wsdlsoap: address location ="Http: // localhost: 8080/axis/HelloWorldService. jws"/>

</Wsdl: port>

</Wsdl: service>

The name and bound information of the webService and the url to access.

 

5. Write the client code below

The Code is as follows:

 

Package com. hoo. client;

Import java. rmi. RemoteException;
Import javax. xml. namespace. QName;
Import javax. xml. rpc. ServiceException;
Import org. apache. axis. client. Call;
Import org. apache. axis. client. Service;
Publicclass HelloWorldClient {

/**
* <B> function: </B> jws axis WebService Client
* @ Author hoojo
* @ CreateDate 2010-12-15 05:10:28 pm
* @ Param args
* @ Throws ServiceException
* @ Throws RemoteException
*/
Publicstaticvoid main (String [] args) throws ServiceException, RemoteException {
// WebService access address
// String url = "http: // localhost: 8080/axis/HelloWorldService. jws ";
String url = "http: // localhost: 8080/AxisWebService/HelloWorldService. jws ";
// Create a service
Service service = new Service ();
// Create a call handle
Call call = (Call) service. createCall ();
// Set the request address
Call. setTargetEndpointAddress (url );
/**
* Set the namespace of the called methods and methods;
* Because it is manually released to the webroot directory, the namespace is consistent with the request address.
* Of course, null is also acceptable, because it does not set a namespace. The general namespace of the method is
* Package name inverted composition, such as com. hoo. service, ns = http://service.hoo.com
*/
Call. setOperationName (new QName (null, "sayHello "));
/**
* Call the sayHello method and set the request parameters. The returned value is the returned value.
*/
String result = (String) call. invoke (new Object [] {"jack", 99 });
System. out. println (result );
}
}

 

 

Analyze the above Code

The url is obtained based on the wsdlsoap: address location information in the xml file. The namespace and method name are based on

 

<Wsdl: operation name = "sayHello">
<Wsdlsoap: operation soapAction = ""/>
-<Wsdl: input name = "sayHelloRequest">
<Wsdlsoap: body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http: // DefaultNamespace" use = "encoded"/>
</Wsdl: input>
-<Wsdl: output name = "sayHelloResponse">
<Wsdlsoap: body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http: // localhost: 8080/axis/HelloWorldJWS. jws" use = "encoded"/>
</Wsdl: output>

 

 

And the detailed information of the Request Parameters and return values is in

 

<Wsdl: message name = "sayHelloRequest">
<Wsdl: part name = "name" type = "xsd: string"/>
<Wsdl: part name = "age" type = "xsd: int"/>
</Wsdl: message>
-<Wsdl: message name = "sayHelloResponse">
<Wsdl: part name = "sayHelloReturn" type = "xsd: string"/>
</Wsdl: message>
-<Wsdl: portType name = "HelloWorldJWS">
-<Wsdl: operation name = "sayHello" parameterOrder = "name age">
<Wsdl: input message = "impl: sayHelloRequest" name = "sayHelloRequest"/>
<Wsdl: output message = "impl: sayHelloResponse" name = "sayHelloResponse"/>
</Wsdl: operation>
</Wsdl: portType>

 

 

You can see in detail.

The call. invoke of the Code is a reflection mechanism in java. If you do not understand it, we recommend that you refer to the content in the jdk documentation java. lang. reflect package.

Run the code above to view the console output:

Jack say: hello world! [Axis] my age is 99

 

Now, axis is complete. Next we don't need to use the official axis project. Let's write our own AxisWebService project, and then let's see it in the published tomcat webapps.

 

6. I just copied the jar package under lib. Now I want to copy the content in web. xml and remove the AdminServlet configuration in it. Others can be retained.

Copy HelloWorldService. java to the webroot directory, remove the package name, and change the suffix to HelloWorldService. jws. (If you are interested, you can check the web-inf directory of the current project published under the tomcat directory to see if there are more.) Finally, you can release the current web project and access http: // localhost: 8080/AxisWebService/HelloWorldService. jws, if you see the same interface as you just saw, proves that you are almost successful. Click the link to see the wsdl xml.

Well, it's not over yet. Let's take a look at the configuration in web. xml. You probably know why.

Web. the most important file in xml is org. apache. axis. transport. http. axisServlet is the central controller of webService. as defined in xml, check whether there are services/*, which indicates that the above access path can also be: http: // localhost: 8080/AxisWebService/services/HelloWorldService

Of course, if you want to write it like this, you need to use the wsdd release method. For details, refer to the following!

 

Reference: http://ws.apache.org/axis/java/user-guide.html

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.