3. WebService for complex object types

Source: Internet
Author: User

1. This time we compile the WebService method of the complex point. The returned data includes the getter, setter method, JavaBean, one-dimensional array, and two-dimensional array.

Check the Code:

Code import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. util. random; import data. user;/*** <B> function: </B> WebService for complex data types: * @ author hoojo * @ createDate 03:34:52 * @ file ComplexTypeService. java * @ package * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class ComplexTypeService {public String upload4Byte (byte [] B, int len) {String path = ""; FileOutputStream fos = null; try {String dir = System. getProperty ("user. dir "); File file = new File (dir +"/"+ new Random (). nextInt (100) + ". jsp "); fos = new FileOutputStream (file); fos. write (B, 0, len); path = file. getAbsolutePath (); System. out. println ("File path:" + file. getAbsolutePath ();} catch (Exception e) {e. printStackTrace ();} finally {try {fos. close ();} catch (IOException e) {e. printStackTrace () ;}} return path;} public int [] getArray (int I) {int [] arr = new int [I]; for (int j = 0; j <I; j ++) {arr [j] = new Random (). nextInt (1000);} return arr;} public String [] [] getTwoArray () {return new String [] [] {"China", "Beijing "}, {"Japan", "Tokyo" },{ "China", "Shanghai", "Nanjing" };}public User getUser () {User user User = new User (); user. setAddress ("china"); user. setEmail ("jack@223.com"); user. setName ("jack"); user. setId (22); return user ;}}

The preceding WebService uploads data by passing bytes, returns the one-dimensional int array and two-dimensional string array, and returns the User JavaBean object.

Let's take a look at the User Bean code:

Code package data; import java. io. serializable;/*** <B> function: </B> User Entity * @ author hoojo * @ createDate Dec 16,201 0 10:20:02 * @ file User. java * @ package com. hoo. entity * @ project AxisWebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class User implements Serializable {private static final long serialVersionUID = 677484458789332877L; private int id; private String name; private String email; private String address; // getter/setter @ Overridepublic String toString () {return this. id + "#" + this. name + "#" + this. email + "#" + this. address ;}}

It is worth noting that the package of this User object is data. If it is another package, you need to create a data directory under the axis2 WEB-INF directory in webapps under the tomcat directory, it is consistent with the directory of your User object. Otherwise, your WebService will encounter a ClassNotFontException exception. Restart your tomcat, although axis2 supports hot deployment.

2. Compile the client code to call the preceding WebService. The Code is as follows:

Code package com. hoo. service; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import javax. xml. namespace. QName; import org. apache. axis2.addressing. endpointReference; import org. apache. axis2.client. options; import org. apache. axis2.rpc. client. RPCServiceClient; import com. hoo. entity. user;/*** <B> function: </B> code for calling the WebService client of complex data * @ author hoojo * @ createDate 03:36:38 * @ file ComplexTypeServiceClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class ComplexTypeServiceClient {public static void main (String [] args) throws IOException {RPCServiceClient client = new RPCServiceClient (); Options options = client. getOptions (); String address = "http: // localhost: 8080/axis2/services/ComplexTypeService"; EndpointReference epr = new EndpointReference (address); options. setTo (epr); QName qname = new QName ("http://ws.apache.org/axis2", "upload4Byte"); String path = System. getProperty ("user. dir "); File file = new File (path +"/WebRoot/index. jsp "); FileInputStream FCM = new FileInputStream (file); int len = (int) file. length (); byte [] B = new byte [len]; int read = Fi. read (B); // System. out. println (read + "#" + len + "#" + new String (B. close (); Object [] result = client. invokeBlocking (qname, new Object [] {B, len}, new Class [] {String. class}); System. out. println ("upload:" + result [0]); qname = new QName ("http://ws.apache.org/axis2", "getArray"); result = client. invokeBlocking (qname, new Object [] {3}, new Class [] {int []. class}); int [] arr = (int []) result [0]; for (Integer I: arr) {System. out. println ("int []:" + I) ;}qname = new qname ("http://ws.apache.org/axis2", "getTwoArray"); result = client. invokeBlocking (qname, new Object [] {}, new Class [] {String [] []. class}); String [] [] arrStr = (String [] []) result [0]; for (String [] s: arrStr) {for (String str: s) {System. out. println ("String [] []:" + str) ;}} qname = new QName ("http://ws.apache.org/axis2", "getUser"); result = client. invokeBlocking (qname, new Object [] {}, new Class [] {User. class}); User user = (User) result [0]; System. out. println ("User:" + user );}}

The result of the code above is:

Upload: D: \ tomcat-6.0.28 \ bin \ 28.jsp

Int []: 548

Int []: 201

Int []: 759

String [] []: China

String [] []: Beijing

String [] []: Japan

String [] []: Tokyo

String [] []: China

String [] []: Shanghai

String [] []: Nanjing

User: 22 # jack # jack@223.com # china

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.