WebService (3)-axis advanced features to pass complex objects

Source: Internet
Author: User
Tags soap reserved wsdl

In addition to passing strings, the client can pass complex objects (objects must be serialized), List,map, arrays, and files.


(1) Defining an object implements the Serializable interface
Package Cn.com.chenlly.ssh.webservice.axis;

Import java.io.Serializable;
Import java.util.ArrayList;
Import java.util.List;

/**
* @Class Address.java
* @Description
* @Copyright (c) 2010, Zheng Chen Technology Co., Ltd. All rights reserved.
* @Author chenlly
* @Version 1.0
* @Date APR, 5:03:23 PM
*/
Public class address implements Serializable {

Private Integer identifier;

Private String address;

Private String City;

Private String Province;

Private String country;

Private String []array;

Private list<integer> List;

Private Boolean isexst;


Constructor
Public address () {

List = new arraylist<integer> ();

List.add (1);

List.add (2);

List.add (3);
}
Public Integer Getidentifier () {
return identifier;
}

public void Setidentifier (Integer identifier) {
This.identifier = identifier;
}

Public String getaddress () {
return address;
}

public void setaddress (String address) {
this.address = address;
}

Public String getcity () {
return to City;
}

public void Setcity (String city) {
this.city = City;
}

Public String getprovince () {
return province;
}

public void Setprovince (String province) {
this.province = Province;
}

Public String Getcountry () {
return country;
}

public void Setcountry (String country) {
This.country = country;
}

Public string[] GetArray () {
return array;
}

public void SetArray (string[] array) {
This.array = array;
}

Public list<integer> getlist () {
return list;
}

public void setlist (list<integer> List) {
This.list = list;
}

public Boolean isexst () {
return isexst;
}

public void Setexst (Boolean isexst) {
This.isexst = isexst;
}
}

(2) Define SERVER-CONFIG.WSDD files

<deployment xmlns= "http://xml.apache.org/axis/wsdd/"
Xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java" >
Type= "Java:org.apache.axis.handlers.http.URLMapper"/>
Type= "Java:cn.com.chenlly.ssh.webservice.axis.WSTestServiceHandle" >
<parameter name= "status" value= "Success"/>
<!--Custom Service-->
<service name= "ws" Provider= "JAVA:RPC" >
<parameter name= "ClassName"
Value= "Cn.com.chenlly.ssh.webservice.axis.WSTestServiceImpl"/>
<parameter name= "Allowedmethods" value= "*"/>
<parameter name= "Scope" value= "request"/>

<responseFlow>
</responseFlow>

<requestFlow>
</requestFlow>

<beanmapping qname= "Mynsd:address"
Xmlns:mynsd= "Urn:addressmanager"
Languagespecifictype= "Java:cn.com.chenlly.ssh.webservice.axis.Address" >
</beanMapping>
</service>

<transport name= "http" >
<requestFlow>
</requestFlow>
</transport>
</deployment>

The main is the name space of <beanMapping> label and QName writing
The first custom namespace and the second node local part will form a new QName.
Open a link in IE
http://192.168.1.98:8082/SSHProject/services/ws?wsdl
The generated WSDL has the following paragraph
-<schema targetnamespace= "Urn:addressmanager" xmlns= "Http://www.w3.org/2001/XMLSchema" >
<import namespace= "Http://192.168.1.98:8082/SSHProject/services/ws"/>
<import namespace= "Http://xml.apache.org/xml-soap"/>
<import namespace= "http://schemas.xmlsoap.org/soap/encoding/"/>
-<complextype name= "Address" >
-<sequence>
<element name= "Address" nillable= "true" type= "soapenc:string"/>
<element name= "Array" nillable= "true" type= "impl:arrayof_soapenc_string"/>
<element name= "City" nillable= "true" type= "soapenc:string"/>
<element name= "Country" nillable= "true" type= "soapenc:string"/>
<element name= "exst" type= "Xsd:boolean"/>
<element name= "identifier" nillable= "true" type= "Soapenc:int"/>
<element name= "List" nillable= "true" type= "Impl:arrayof_xsd_anytype"/>
<element name= "province" nillable= "true" type= "soapenc:string"/>
</sequence>
</complexType>
</schema>
The new schema is to serialize the object and generate the XML stream file.

(3) WebService service method
Public address dealaddress (address) {
SYSTEM.OUT.PRINTLN ("Service exst:" +address.isexst ());
Client object passed over set flag to True
Address.setexst (TRUE);
return address;
}

(4) Client
Package Cn.com.chenlly.ssh.webservice.axis;

Import java.util.ArrayList;
Import java.util.List;

Import Javax.xml.namespace.QName;
Import Javax.xml.rpc.ParameterMode;

Import Org.apache.axis.client.Call;
Import Org.apache.axis.client.Service;
Import Org.apache.axis.encoding.ser.BeanDeserializerFactory;
Import Org.apache.axis.encoding.ser.BeanSerializerFactory;

/**
* @Class Wstestservice.java
* @Description client to invoke remote service
* @Copyright (c) 2010, Zheng Chen Technology Co., Ltd. All rights reserved.
* @Author chenlly
* @Version 1.0
* @Date APR 9, 3:57:26 PM
*/
public class Wstestserviceclient {
public static void Main (string[] args) {
Service service = new service ();
try {
Call call = (call) Service.createcall ();
String url = "HTTP://192.168.1.98:8082/SSHPROJECT/SERVICES/WS?WSDL";
Call.settargetendpointaddress (New Java.net.URL (URL));
Defining objects
Address = new address ();
Address.setidentifier (1);
Address.setprovince ("Hunan");
Address.setcity ("Changsha");
Address.setexst (FALSE);

QName qn = new QName ("Urn:addressmanager", "address");//first parameter name space URI, second parameter local part, Note that these two parts are labeled Beanmapping configured in the SERVER-CONFIG.WSDD file
Call.registertypemapping (Address.class, QN,
New Beanserializerfactory (Address.class, QN),//serialization
New Beandeserializerfactory (Address.class, qn));

Call.setoperationname (new QName (URL, "dealaddress"));

Call.addparameter ("arg0", QN, parametermode.in);//define a parameter type, if it is a string parameter you may not need this sentence
Call.setreturnclass (Address.class);//Specify return type

Address result = (address) Call.invoke (new object[]{address});//The service is passed here by an object
System.out.println (Result.isexst ());
catch (Exception e) {
E.printstacktrace ();
}
}
}
The Isexst () method that is printed on the server side is false, and then sets the exst of the address to true, and finally the result to the client print is True

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.