AXIS2 testing WebService Server and client

Source: Internet
Author: User

First, Environment construction

Download Axis2-1.6.2-war.zip/axis2-1.6.2-bin.zip and more.

Refer to Axis2-1.6.2-war\readme.txt and axis2-1.6.2-war\axis2\web-inf\services\version-1.6.2\meta-inf\ The Services.xml notation can be published by writing the WebService class and packaging it as a jar in the Services directory under the Axis2 WebApp of the Tomcat container.

Test: Copy the Axis2 directory to the%tomcat_home%/webapps directory, start TOMCAT, and Access Http://localhost:8080/axis2, normal.

Second, write the WebService server side

1 beans

Package com.test.entity;

public class Student {

Private String ID;
private String name;
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
@Override
Public String toString () {
Return "Student [id=" + ID + ", name=" + name + "]";
}
}

2 WebService Class

Package com.test;

Import com.test.entity.Student;

public class HelloWorld {

Public String SayHello () {
Return "saying hellooooooooooooo----2----";
}

Public Student query () {
Student s = new Student ();
S.setid ("1");
S.setname ("wsc123");
return s;
}

Public Integer addtownumbers (int a, int b) {
return a+b;
}
}
3 Services.xml

<service name= "HelloWorld" >
<description>
This service was to get the running Axis version
</description>
<parameter name= "ServiceClass" >com.test.HelloWorld</parameter>
<operation name= "SayHello" >
<messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name= "Query" >
<messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name= "Addtownumbers" >
<messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>

<!--
<service name= "HelloWorld" scope= "Application" >
<description>
Pojo:helloworld Service
</description>
<messageReceivers>
<messagereceiver mep= "Http://www.w3.org/2004/08/wsdl/in-only"
class= "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messagereceiver mep= "Http://www.w3.org/2004/08/wsdl/in-out"
class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name= "ServiceClass" >com.test.HelloWorld</parameter>
</service>
-

4 Build.xml

<project name= "HelloWorldService" basedir= "." default= "Deploy" >

<property name= "Src.dir" value= "src" >
</property>
<property name= "Build.dir" value= "${basedir}/build" >
</property>

<path id= "Build.classpath" >
</path>

<target name= "Init" >
<delete dir= "${build.dir}" >
</delete>
<mkdir dir= "${build.dir}"/>
<mkdir dir= "${build.dir}/classes"/>
<mkdir dir= "${build.dir}/jar"/>
</target>

<target name= "Compile" depends= "init" >
<javac srcdir= "${src.dir}" destdir= "${build.dir}\classes" >
<classpath refid= "Build.classpath" >
</classpath>
</javac>
</target>

<target name= "Makejar" depends= "compile" >
<jar destfile= "${build.dir}\jar\${ant.project.name}.jar" >
<fileset dir= "${build.dir}/classes" >
<include name= "**/*.class"/>
</fileset>
<metainf dir= "${basedir}" >
<include name= "Services.xml"/>
</metainf>
</jar>
</target>

<target name= "Deploy" depends= "Makejar" >
<copy file= "${build.dir}/jar/${ant.project.name}.jar" todir= "D:\wsc\software\apache-tomcat-6.0.41\webapps\ Axis2\web-inf\services "></copy>
</target>

</project>

5 Readme.txt

Publish a WebService to the Tomcat container in a pojo way.
It is important to note that:
The format of the jar made by ant, the Meta-inf directory must include the Services.xml,
And Services.xml, you can assume that each method is a pair of operation tags.

Third, write RPC client side

1 References Required Axis2-1.6.2-war\axis2\web-inf\lib\*.jar

2 bean/entity

Package com.test.entity;

public class Student {
Private String ID;
private String name;
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
@Override
Public String toString () {
Return "Student [id=" + ID + ", name=" + name + "]";
}
}
3 RPC Client Class

Package sample.addressbook.rpcclient;

Import Javax.xml.namespace.QName;

Import Org.apache.axis2.AxisFault;
Import org.apache.axis2.addressing.EndpointReference;
Import org.apache.axis2.client.Options;
Import org.apache.axis2.rpc.client.RPCServiceClient;

Import com.test.entity.Student;

public class Helloworldrpcclient {

public static void Main (string[] args1) throws Axisfault {
Rpcserviceclient serviceclient = new Rpcserviceclient ();
Options options = Serviceclient.getoptions ();
EndpointReference Targetepr = new EndpointReference ("Http://127.0.0.1:8080/axis2/services/HelloWorld");
Options.setto (TARGETEPR);
QName of the target method
QName QName = new QName ("http://test.com", "SayHello");
Constructing the arguments array for the method invocation
object[] Opargs = new object[] {};
class[] Returntypes = new class[] {string.class};
Invoking the method
object[] Response = serviceclient.invokeblocking (QName, Opargs, returntypes);
System.out.println (Response[0]);

How do I return an object??
//
QName = new QName ("http://test.com", "Query");
Opargs = new object[]{};
Returntypes = new class[] {student.class};
Response = serviceclient.invokeblocking (QName, Opargs, returntypes);
System.out.println (Response[0]);

How do I pass parameters to the Web service method?
QName = new QName ("http://test.com", "addtownumbers");
Opargs = new Object[]{integer.valueof (3), integer.valueof (4)};
Opargs = new object[]{33, 44};
Returntypes = new Class[]{integer.class};
Response = serviceclient.invokeblocking (QName, Opargs, returntypes);
System.out.println (Response[0]);


}
}

4 Running Results

Log4j:warn No Appenders could is found for logger (org.apache.axis2.context.AbstractContext).
Log4j:warn Initialize the log4j system properly.
Saying hellooooooooooooo----2----
Student [Id=1, name=wsc123]
77

5 Readme.txt

Refer to the Pojo example in the Samples directory of the Axis2
The focus is on the Sample.addressbook.rpcclient.HelloWorldRPCClient class, which is used to access its own published HelloWorld, the WebService.

Iv. issues

Can clients be accessed in many forms, such as ADB?

AXIS2 supports multiple protocols, and HTTP should be the most common. And also?

AXIS2 testing WebService Server and client

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.