Axis2 series tutorial for WebService (iii) Transmission of axis2 Composite data

Source: Internet
Author: User

In practical applications, we need not only to use WebService to transmit simple data types, but also to transmit more complex data, which can be called composite data. Arrays and classes (interfaces) are commonly used composite types. In axis2, you can directly declare the parameters or return values of the WebService method as an array or a class (interface ). Note that only one-dimensional arrays can be used to define the array type. To pass multi-dimensional arrays, you can use separators to separate them, as shown in the following code:

String [] strarray = new string [] {"bicycle, airplane, Rocket", "China, United States, Germany", "Superman, Spider, Iron Man "};

The Code can be considered as a 3*3 two-dimensional array.

When passing an object instance of a class, in addition to directly declaring the array type as a corresponding class or interface, you can also serialize the object instance, that is, converts an object instance to a byte array for transmission, and then the receiver deserializes it to restore the object instance.

The following sample code demonstrates how to transmit data of the array and class (Interface) Types and how to upload images using Byte arrays. The client code in this example is written in Java. The following steps are required to complete this example:

1. Implement server code

Complextypeservice is a WebService class. The code for this class is as follows:

Import Java. io. fileoutputstream; import data. dataform; public class complextypeservice {// uploads an image. The imagebyte parameter indicates the bytes of the uploaded image file, // The length parameter indicates the byte length of the image file (the value of this parameter may be less than the length of the imagebyte array) Public Boolean uploadimagewithbyte (byte [] imagebyte, int length) {fileoutputstream Fos = NULL; try {// Save the uploaded image to the test1.jpg file of drive D. Fos = new fileoutputstream ("D: \ test1.jpg"); // write the byte FOS of the image file. write (imagebyte, 0, length); FOS. close ();} Catch (exception e) {return false;} finally {If (FOS! = NULL) {try {FOS. close () ;}catch (exception e) {}} return true;} // returns a one-dimensional string array Public String [] getarray () {string [] strarray = new string [] {"bicycle", "airplane", "Rocket"}; return strarray ;} // returns the Two-Dimensional String Array Public String [] getmdarray () {string [] strarray = new string [] {"bicycle, airplane, Rocket", "China, United States, Germany ", "Superman, Spider, Iron Man"}; return strarray;} // return the dataform class object instance public dataform getdataform () {return New dataform ();} // serialize the object instance of the dataform class and return the serialized byte array public byte [] getdataformbytes () throws exception {Java. io. bytearrayoutputstream baos = new Java. io. bytearrayoutputstream (); Java. io. objectoutputstream OOS = new Java. io. objectoutputstream (baos); OOS. writeobject (New dataform (); Return baos. tobytearray ();}}
2. Implement the dataform class

Dataform is the class corresponding to the object instance to be returned. The implementation code of this class is as follows:

       package data;              public class DataForm implements java.io.Serializable {              private String name = "bill";              private int age = 20;                            public String getName() {                     return name;              }              public void setName(String name) {                     this.name = name;              }              public int getAge() {                     return age;              }              public void setAge(int age) {                     this.age = age;              }       }
3. Publish WebService

Because the WebService class in this example uses a Java class (dataform class), before publishing WebService, you must first import dataform. copy the class file to the <tomcat installation directory> \ webapps \ axis2 \ WEB-INF \ Classes \ data directory, and then complete xtypeservice. copy the class file to the <tomcat installation directory> \ webapps \ axis2 \ WEB-INF \ pojo directory, and finally start Tomcat (if tomcat has been started, because a dataform class is added, tomcat needs to be restarted ).

4. Use Java to compile the client code for WebService call

The RPC call method is still used on the client. The Code is as follows:

Package client; import javax. XML. namespace. QNAME; import Org. apache. axis2.addressing. endpointreference; import Org. apache. axis2.client. options; import Org. apache. axis2.rpc. client. rpcserviceclient; public class complextyperpcclient {public static void main (string [] ARGs) throws exception {rpcserviceclient serviceclient = new rpcserviceclient (); options Options = serviceclient. getoptions (); endpointreference targetepr = new endpointreference ("http: // localhost: 8080/axis2/services/complextypeservice"); options. setto (targetepr); // 1. the following code calls the uploadimagewithbyte method to upload an image file // open the image file and determine the size of the image file Java. io. file file = new Java. io. file ("F: \ images.jpg"); Java. io. fileinputstream FCM = new Java. io. fileinputstream ("F: \ images.jpg"); // create a byte array byte [] buffer = new byte [(INT) file to save the content of the image file to be uploaded. length ()]; // read the content of the image file in the buffer array. read (buffer); system. out. println ("file length:" + file. length (); object [] opaddentryargs = new object [] {buffer, n}; class [] classes = new class [] {Boolean. class}; QNAME opaddentry = new QNAME ("http://ws.apache.org/axis2", "uploadimagewithbyte. close (); // start to upload the image file and output the response of the uploadimagewithbyte method to system. out. println (serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0]); // 2. the following code calls the getarray method and returns the One-Dimensional String Array opaddentry = new QNAME ("http://ws.apache.org/axis2", "getarray"); string [] strarray = (string []) serviceclient. invokeblocking (opaddentry, new object [] {}, new class [] {string []. class}) [0]; for (string S: strarray) system. out. print (S + ""); system. out. println (); // 3. the following code calls the getmdarray method and returns the One-Dimensional String Array opaddentry = new QNAME ("http://ws.apache.org/axis2", "getmdarray"); strarray = (string []) serviceclient. invokeblocking (opaddentry, new object [] {}, new class [] {string []. class}) [0]; for (string S: strarray) {string [] array = S. split (","); For (string SS: array) system. out. print ("<" + SS + ">"); system. out. println ();} system. out. println (); // 4. the following code calls the getdataform method and returns the dataform object instance opaddentry = new QNAME ("http://ws.apache.org/axis2", "getdataform"); data. dataform df = (data. dataform) serviceclient. invokeblocking (opaddentry, new object [] {}, new class [] {data. dataform. class}) [0]; system. out. println (DF. getage (); // 5. the following code calls the getdataformbytes method and returns the byte array, // finally deserializes the returned byte array, converts it to the dataform object instance opaddentry = new QNAME ("http://ws.apache.org/axis2 ", "getdataformbytes"); buffer = (byte []) serviceclient. invokeblocking (opaddentry, new object [] {}, new class [] {byte []. class}) [0]; Java. io. objectinputstream OIS = new Java. io. objectinputstream (New Java. io. bytearrayinputstream (buffer); df = (data. dataform) Ois. readobject (); system. out. println (DF. getname ());}}

Run the above program and output the following content:

File length: 3617 true bicycle airplane rocket <bicycle> <aircraft> <rocket> <China> <United States> <Germany> <Superman> <spider> <iron Xia> 20 bill

 

 

 

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.