The first example of using axis2 for WebService beginners

Source: Internet
Author: User
Tags wsdl

 

You need to use WebService in the project, so take a moment to study it.

Currently, the popular WebService frameworks include axis2 (1 and 2 are completely different), xfire, and cxf.

Try axis2 first. Download the jar package and war package from the Internet.

Websevice released by axis2 on the Internet seems to be the same example, and

Manually released separately, so study the official war package on your own.

Poor Chinese materials, English is very important, or Google translation is always needed.

 

Check out the extracted axis2.

The axis2-web is a JSP page, so here we should be able to see how to write the URL request WebService,

Wait and check again.

Org contains the class file,

Next Look At The WEB-INF, this is very important

Web. xml: copy the content to the Web. xml file of your project.

The content is configured with sevlet.

The corresponding services and modules are also copied to the project's WEB-INF.

Conf, which has an axis2.xml, which is also copied to the WEB-INF.

The jar package in lib must be copied.

This completes the preparation.

 

For axis2.xml, there is

<Parameter name = "hotdeployment"> true </parameter> Hot release
<Parameter name = "hotupdate"> false </parameter> Hot update

Change the hot update to true so that WebService can be published without restarting the server.

I have never tried this. Study it later.

 

Write a Test Service.

Package lsy.com; public class higirl {Public String sayhi (string Str) {return STR + ", hello";} public void goodfbye (string Str) {system. out. println (STR + "-- bye ");}}

This class is very simple. There are two methods: sayhi and goodfbye.

Refresh the project and go to the workspace to find the compiled class file.

There is an example version-1.6.2.arr in the Services Directory, this example is very good, with reference to it we can easily write a WebService.

 

After decompression, there are 2 directories, A META-INF, a class package name (which is a class file ).

I copied a META-INF, and the class is the higirl. Class I just compiled.

, Create its package according to this class (make sure to create the corresponding package lsy.com );

Then open services. xml under the META-INF, and other files are not important.

Rewrite the service to ours.

<!--  ~ Licensed to the Apache Software Foundation (ASF) under one  ~ or more contributor license agreements. See the NOTICE file  ~ distributed with this work for additional information  ~ regarding copyright ownership. The ASF licenses this file  ~ to you under the Apache License, Version 2.0 (the  ~ "License"); you may not use this file except in compliance  ~ with the License. You may obtain a copy of the License at  ~  ~ http://www.apache.org/licenses/LICENSE-2.0  ~  ~ Unless required by applicable law or agreed to in writing,  ~ software distributed under the License is distributed on an  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  ~ KIND, either express or implied. See the License for the  ~ specific language governing permissions and limitations  ~ under the License.  --><service name="ServiceFirst">    <description>        first    </description>    <parameter name="ServiceClass">com.lsy.HiGirl</parameter>    <operation name="sayHi">    <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />    </operation></service>

 

The service request is servicefirst,

The request method is sayhi; the message receiving type is the one with the return value of the parameter (Org. Apache. axis2.rpc. Receivers. rpcmessagereceiver)

This type can be defined in axis2.xml.

After modification, You Need To package these two folders into AAR.

How to package it? Eclipse has related plug-ins that can be downloaded from Apache on the axis official website. I have not tested them yet.

In fact, you can compress it into a zip file and rename it to an AAR file.

The name of the compressed package is useless, for example, A. AAR.

Then copy A. AAR to webcontent/WEB-INF/services,

Restart the project and enter

HTTP: /localhost: 8080/axistest/services/listservices

This is the servlet that comes with axis2. You can return the list of currently available WebServices.

The second servletfirst is just written by me.

Right-click to view the address

Http: // localhost: 8080/axistest/services/servicefirst? WSDL

 

Return Value:

  <?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://lsy.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://lsy.com">  <wsdl:documentation>ServiceFirst</wsdl:documentation> - <wsdl:types>- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://lsy.com">- <xs:element name="sayHi">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="str" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>- <xs:element name="sayHiResponse">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>- <xs:element name="goodfBye">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="str" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>  </xs:schema>  </wsdl:types>- <wsdl:message name="sayHiRequest">  <wsdl:part name="parameters" element="ns:sayHi" />   </wsdl:message>- <wsdl:message name="sayHiResponse">  <wsdl:part name="parameters" element="ns:sayHiResponse" />   </wsdl:message>- <wsdl:message name="goodfByeRequest">  <wsdl:part name="parameters" element="ns:goodfBye" />   </wsdl:message>- <wsdl:portType name="ServiceFirstPortType">- <wsdl:operation name="sayHi">  <wsdl:input message="ns:sayHiRequest" wsaw:Action="urn:sayHi" />   <wsdl:output message="ns:sayHiResponse" wsaw:Action="urn:sayHiResponse" />   </wsdl:operation>- <wsdl:operation name="goodfBye">  <wsdl:input message="ns:goodfByeRequest" wsaw:Action="urn:goodfBye" />   </wsdl:operation>  </wsdl:portType>- <wsdl:binding name="ServiceFirstSoap11Binding" type="ns:ServiceFirstPortType">  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="sayHi">  <soap:operation soapAction="urn:sayHi" style="document" /> - <wsdl:input>  <soap:body use="literal" />   </wsdl:input>- <wsdl:output>  <soap:body use="literal" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="goodfBye">  <soap:operation soapAction="urn:goodfBye" style="document" /> - <wsdl:input>  <soap:body use="literal" />   </wsdl:input>  </wsdl:operation>  </wsdl:binding>- <wsdl:binding name="ServiceFirstSoap12Binding" type="ns:ServiceFirstPortType">  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="sayHi">  <soap12:operation soapAction="urn:sayHi" style="document" /> - <wsdl:input>  <soap12:body use="literal" />   </wsdl:input>- <wsdl:output>  <soap12:body use="literal" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="goodfBye">  <soap12:operation soapAction="urn:goodfBye" style="document" /> - <wsdl:input>  <soap12:body use="literal" />   </wsdl:input>  </wsdl:operation>  </wsdl:binding>- <wsdl:binding name="ServiceFirstHttpBinding" type="ns:ServiceFirstPortType">  

Here we can see some information, there is a targetnamespace ="Http://lsy.com

My higirl. Java package name is com. lsy

This is the parameter we use in the client QNAME.

You can also use this method to view the content:

Http: // localhost: 8080/axistest/services/servicefirst? XSD

Return Value:

- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://lsy.com" xmlns:ns1="http://org.apache.axis2/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://lsy.com">- <xs:element name="sayHi">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="str" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>- <xs:element name="sayHiResponse">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>- <xs:element name="goodfBye">- <xs:complexType>- <xs:sequence>  <xs:element minOccurs="0" name="str" nillable="true" type="xs:string" />   </xs:sequence>  </xs:complexType>  </xs:element>  </xs:schema>

The URL is followed? Later, I think the access standards are different.

WSDL is a standard, and XSD is also a standard ).

Regardless of the standard or format, the request and return can be clearly stated. We do not care about the process, and how to use it is the focus.

The parameters, types, return values, and types of namespaces, methods, and methods are clearly described.

 

Axis2-web Li has a page index. jsp

Access http: // localhost: 8080/axistest/axis2-web/index. jsp

This is the overview page. Click service data to go to the available service list page.

So that axis2-web JSP page package is still useful, Web. xml inside the configuration of other servlet we 'd better not note out,

In this way, the test is more convenient. You can see the available services on the page, and the access address is also

Clear.

 

My firstSayhiThere is a returned value, access the following

Http: // localhost: 8080/axistest/services/servicefirst? Sayhi

Or http: // localhost: 8080/axistest/services/servicefirst/sayhi

Return

-<Ns: sayhiresponse xmlns: NS = "http://lsy.com"> <ns: Return> null, hello </ns: Return> </ns: sayhiresponse>

If you want to pass the parameter to WebService, write the address as follows:

Http: // localhost: 8080/axistest/services/servicefirst/sayhi? STR = QQ

The parameter name is the parameter name of the method you write. Once written, the parameter name is fixed. For example:

Public String sayhi (string Str ){
Return STR + ", hello ";
}

This parameter is called STR, which is also used for access.

Return Value:

-<Ns: sayhiresponse xmlns: NS = "http://lsy.com"> <ns: Return> QQ, hello </ns: Return> </ns: sayhiresponse>

 

 

 

In this way, wbservice can be accessed, while WebService is a program service, so you need to write the client access code in the program.

Clientfirst. Java

 

Package com; import javax. XML. namespace. QNAME; import Org. apache. axis2.axisfault; import Org. apache. axis2.addressing. endpointreference; import Org. apache. axis2.client. options; import Org. apache. axis2.client. serviceclient; import Org. apache. axis2.rpc. client. rpcserviceclient; public class clientfirst {public static void main (string [] ARGs) {// return value of the external interface string res = ""; try {rpcserviceclient service = new rpcserviceclient (); options o = service. getoptions (); string url = "http: // localhost: 8080/axistest/services/servicefirst"; endpointreference ER = new endpointreference (URL); O. setto (ER); QNAME Qn = new QNAME ("http://lsy.com", "sayhi"); object [] Param = new object [] {"Haha "}; class [] retype = new class [] {string. class}; object result [] = service. invokeblocking (qN, Param, retype); system. out. println ("Return Value:" + result [0]. tostring ();} catch (axisfault e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Note that there are many classes with duplicate package names, which must be introduced during writing.

Print:

Log4j: warn no appenders cocould be found for logger (Org. Apache. axis2.context. abstractcontext ).
Log4j: Warn please initialize the log4j system properly.
The returned value is Haha.

 

 

In this way, the return value is returned.

 

The project structure is as follows:

 

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.