SUVABean call instance practice (transfer)

xiaoxiao2021-03-06  22

SUAP Mixed Type (Javabean) Practice

Tools: AXIS-1_1

Tomcat 5.2.x

Ide: Eclipse 3.1

First, simple start:

1. Create a Javabean class Student.java

Package com.kevingq.service.axis.model;

Import java.io.serializable;

Public Class Student IMPLEments Serializable {

Private string _name;

PRIVATE STRING_ID;

PRIVATE STRING _COMMENT;

Public student () {}

Public student (String name, string id, string ") {

_name = name;

_id = id;

_Comment = Comment;

}

Public string getname () {

Return_name;

}

Public void setname (String name) {

_name = name;

}

Public string getId () {

Return_ID;

}

Public void setid (String ID) {

_id = id;

}

Public String getcomment () {

Return_Comment;

}

Public void setcomment (string commer) {

_Comment = Comment;

}

}

2, write the service program

Package com.kevingq.service.axis.service;

Import com.kevingq.service.axis.model.student;

Public class getstudentservice {

Public Student GetaStud (String Name) {

Student a = new student ("a", "10001", "i'm a");

Return A;

}

}

3, deploy AXIS and deploy service

a. Copy the AXIS-1_1 / WebApps / Axis folder from Axis-1_1.zip to Tomcat 5.0.x / WebApps /

b. Open WebApps / Axis / Web-INF / Server-Config.WSDD for editing, inserting the following piece under the tab

STUDENTINFOSERVICE is the name of this web service, and it needs to be used when the client is encoded.

The class provided by this service is described, including the full class of Package.

What are the methods available to external calls, * indicate all functions, now you can change * to getaStudent.

Description How to perform Serializing it for this JavaBean And DE-Serializing, the purpose of the description is to bind the object category of JavaBean. Pay attention to the namespace illustrated in the label. This label is actually a shorthand of the following tag:

LanguagespecificType = "java: com.kevingq.service.axis.model.student"

Serializer = "org.apache.axis.encoding.ser.beanserializerfactory"

Deserializer = "Org.apache.axis.encoding.ser.beandeserializerfactory"

EncodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" />

c. Put the compiled student.class and getStudentService.class (within their respective packages) to axis / web-inf / classes /.

4, start Tomcat, visit http: // localhost: 8080 / axis / admin.html, check your deployment service

5, write the client

I am importing AXIS-1_1 / lib / in addition to AXIS_ANT.JAR, in addition to Axis_ant.jar, in Eclipse.

Package com.kevingq.service.axis.client;

Import java.net.URL;

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.xmltype;

Import org.apache.axis.encoding.ser.beandeserializerfactory;

Import org.apache.axis.encoding.ser.beanserializerfactory;

Import com.kevingq.service.axis.model.student; public class getastudentclient {

Public static void main (string [] args) Throws Exception

{

Service service = new service ();

Call call = (call) service.createcall ();

QNAME Qn = New QNAME ("URN: StudentInfoservice", "student");

Call.registertypemapping (student.class, qn,

New BeanserializerFactory (Studient.class, Qn),

New BeandeSerializerFactory (student.class, qn)

);

Try {

Call.SettargetendPointd Address ("http: // localhost: 8080 / axis / service / studentService"));

Call.Setoperationname (New QName ("StudentInfoservice", "GetaStudent"));

Call.addparameter ("arg1", xmltype.xsd_string, parametermode.in);

Call.setReturnclass (student.class);

Student a = (student) Call.invoke (New Object [] {"a"});

System.out.println (A.GetId ());

} catch (exception e) {

System.out.println ("Error:" E.TOString ());

}

}

}

The red code section indicates any string because the parameters of the GetaStudent method have no effect on the result of the return, which is just symbolic delivery of a parameter. The bold part is a need to explain the JavaBean category to serialize and de-serialize in the Client side, and the instructions for the parameters can refer to the Axis API documentation.

To get the result of the run, the client needs to get a Student.Class file, but how to get this student.class if a service that is not in this machine? - You need to read this WebService's WSDL document, there is a description of the domains in this Javabean object, according to JavaBean's coding specification, you have written compilation to the student.class file.

Second, slightly in-depth

What do I want to get a student array?

You only make a change in modification:

1, a new class of the server STUDENTLIB.JAVA

Package com.kevingq.service.axis.model;

Import java.util.arraylist;

Public class studentlib {

ArrayList StudENTLIB = NULL;

Public studentlib () {

StudENTLIB = New ArrayList ();

}

Public void addstudent (student s) {

StudENTLIB.ADD (s);

}

Public ArrayList GetStudents (String Name, String ID) {

ArrayList List = New ArrayList ();

For (int i = 0; i

&& this.get (i) .GetId (). Equals (ID)) {

List.add (this.get (i));

}

}

Return List;

}

Public student get (int index) {

Return (Student) StudentLib.get (INDEX);

}

}

This class is only written in order to achieve a slightly complex point logic function. Note that the getStudents method returns a reference to the ArrayList type. Since the data type supported in SOAP contains ArrayList in Java, it is convenient to use this type.

2, extend the service program

Package com.kevingq.service.axis.service;

Import java.util.arraylist;

Import com.kevingq.service.axis.model.student;

Import com.kevingq.service.axis.model.studentlib;

Public class getstudentservice {

Public arraylist getstudent () {

ArrayList Students = New ArrayList ();

Student a = new student ("a", "10001", "i'm a");

Student B = New Student ("A", "10002", "I'm B");

Student C = New Student ("A", "10001", "I'm A, I'm Not C");

StudENTLIB LIB = New StudENTLIB ();

Lib.Addstudent (a);

Lib.Addstudent (b);

Lib.Addstudent (C);

Students = lib.getstudents ("a", "10001");

Return Students;

}

Public Student GetaStud (String Name) {

Student a = new student ("a", "10001", "i'm a");

Return A;

}

}

A new way to add a new way, let's describe it on the server.

3, deploy service

Take a look at the piece that just added to Server-config.wsdd, it seems that you don't have to modify (as long as you have the way to expose the exposed method *)

4, write a client to see

Package com.kevingq.service.axis.client;

Import java.net.URL;

Import java.util.arraylist;

Import org.apache.axis.client.call;

Import org.apache.axis.client.service;

Import org.apache.axis.encoding.xmltype;

Import org.apache.axis.encoding.ser.beandeserializerfactory;

Import org.apache.axis.encoding.ser.beanserializerfactory;

Import com.kevingq.service.axis.model.student; import javax.xml.namespace.qname;

Import javax.xml.rpc.parametermode;

Public class getstudentclient {

Public static void main (string [] args) Throws Exception

{

Service service = new service ();

Call call = (call) service.createcall ();

QNAME Qn = New QNAME ("URN: StudentInfoservice", "student");

Call.registertypemapping (student.class, qn,

New BeanserializerFactory (Studient.class, Qn),

New BeandeSerializerFactory (student.class, qn);

Try {

Call.SettargetendPointd Address ("http: // localhost: 8080 / axis / service / studentService"));

Call.SetoperationName (New QName ("StudentInfoservice", "getStudent");

;

Call.setReturnclass (arraylist.class);

ArrayList Result = (arraylist) Call.Invoke (new object [] {};

For (int i = 0; i

Student Stu = (student) Result.get (i);

System.out.println (stu.getname () " stu.getId () " " stu.getcomment ());

}

} catch (exception e) {

System.out.println ("Error:" E.TOString ());

}

}

}

It is very similar to the first client. Note that the type returned by the call is ArrayList to see the code in the code!

The result output 2 records, as expected. Otherwise, you try.

Attachment: WSDL.XML < / WSDL: Input>

转载请注明原文地址:https://www.9cbs.com/read-60139.html

New Post(0)