WebService axis2 series tutorial (4) use the services. xml file to publish WebService

Source: Internet
Author: User

Using axis2 to implement web service, although pojo classes can be directly published into the axis2 \ WEB-INF \ pojo directory
Service, but these pojo classes cannot be in any package. This seems inconvenient. Therefore, axis2 allows publishing pojo classes with packages to the Web
Service.
First, implement a pojo class. The Code is as follows:

Package Service; public class myservice {Public String getgreeting (string name) {return "hello" + name;} public void Update (string data) {system. out. println ("<" + Data + "> updated ");}}

This class has two methods, both of which must be published as Web service methods. This method is different from the pojo class directly in the pojo directory. To publish the myservice class to the Web
Service, need a services. xml file, this file needs to be placed in the META-INF directory, the content of the file is as follows:

<Service name = "myservice"> <description> Web service example </description> <parameter name = "serviceclass"> service. myservice </parameter> <messagereceivers> <messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-out" class = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> <messagereceiver MEP =" http://www.w3.org/2004/08/wsdl/in-only "class =" org. apache. axis2.rpc. receivers. rpcinonlymessagereceiver "/> </messagereceivers> </service>

The <service> element is used to publish a web service. A <service> element can only publish one WebService class. The name attribute indicates the WebService name. the following URL can obtain the WSDL content of the WebService:

Http: // localhost: 8080/axis2/services/myservice? WSDL

The name attribute name is "?" In the above URL "? "And.

The <description> element indicates the description of the current web service. The <parameter> element is used to set WebService parameters. Here, it is used to set the class name corresponding to WebService. The <messagereceivers> element is used to set the processor used to process WebService methods. For example, the getgreeting method has a return value. Therefore, you need to use the rpcmessagereceiver class that can process input and output, but the update method does not return values. Therefore, you need to use the rpcinonlymessagereceiver class that can only process.

To publish a WebService in this way, you must package it into a. AAR file. The. AAR file actually changes the. jar file extension. Two files are created: myservice. Java and services. xml. Compile myservice. Java to generate myservice. Class. The location of services. xml and myservice. class files is as follows:

D: \ ws \ service \ myservice. Class

D: \ ws \ META-INF \ Services. xml

Go to the WS directory in the Windows console and enter the following command to generate the file. AAR file (in fact ,. the jar file can also publish WebService, but it is recommended in the official axis2 documentation. AAR file publishing WebService ):

jar cvf ws.aar .    jar cvf AxisTest.aar .

Finally copy the WS. AAR file to the <tomcat installation directory> \ webapps \ axis2 \ WEB-INF \ Services Directory, start Tomcat, you can call this WebService. The call method is similar to the method described in WebService that uses pojo to implement 0 configuration.

In addition, you can directly specify WebService class methods in the services. xml file. For example, you can use the following configuration code to publish WebService:

<Service name = "myservice"> <description> Web service example </description> <parameter name = "serviceclass"> service. myservice </parameter> <operation name = "getgreeting"> <messagereceiver class = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> </Operation> <operation name =" Update "> <messagereceiver class =" org. apache. axis2.rpc. receivers. rpcinonlymessagereceiver "/> </Operation> </service>

The previous sections of the configuration code above and the previous services. the content of the XML file is the same, but the <operation> element is used later to specify each WebService method, and the processor for processing each method is specified separately. For the client, the two services are called. the WebService published in the XML file is not much different, but the second service is used. after WebServices is published in an XML file, the string type of the update method is encapsulated in the update class when the wsdl2java command or the stub of the client is generated using C # and Delphi, when passing the parameters of the update method, you must create an object instance of the update class. The WebService released using the first services. xml file can directly pass string-type parameters for the update method when stub is generated. From this point, we can see that the WSDL generated by the two methods is different. However, if the client program uses the first service. when the WebService published in the XML file generates the stub class (the parameter of the update method is string), it is changed to the second service on the server. XML file to publish WebService. In this case, the client does not need to regenerate the stub class, but can directly call the update method. That is to say, the method used by the server to publish WebService does not affect the client.

To publish multiple WebServices, you can use the <ServiceGroup> element. For example, create another myservice1 class. The Code is as follows:

       package service       public class MyService1 {              public String getName()  {                      return "bill";              }       }

In the services. xml file, you can use the following configuration code to configure the myservice and myservice1 classes:

<ServiceGroup> <service name = "myservice"> <description> Web service example </description> <parameter name = "serviceclass"> service. myservice </parameter> <messagereceivers> <messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-out" class = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> <messagereceiver MEP =" http://www.w3.org/2004/08/wsdl/in-only "class =" org. apache. axis2.rpc. receivers. rpcinonlymessagereceiver "/> </messagereceivers> </service> <service name =" myservice1 "> <description> Web service example </description> <parameter name =" serviceclass "> service. myservice1 </parameter> <messagereceivers> <messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-out" class = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> <messagereceiver MEP =" http://www.w3.org/2004/08/wsdl/in-only "class =" org. apache. axis2.rpc. receivers. rpcinonlymessagereceiver "/> </messagereceivers> </service> </ServiceGroup>

 

 

 

 

Download the tutorial source code:

Axistest

Axisspring

Axisproject

 

Reprint please indicate the source http://blog.csdn.net/shimiso

Welcome to our technical exchange group: 173711587

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.