Writing Java Client ( General )

Now we will create a java class which will call our service using the stubs produced by the tool in last section.
We created the stubs from the WSDL file and WSDL from our TestService java class.
Lets remind some terms from TestService java class
ClassName : TestService
Function Name : serviceFunction
Input Parameters : arg1,arg2
Return Type : DataBean

Lets first see how the client code will look like then I will explain it line by line :

package webservice1;

public class TestServiceClient {
public static void main(String[] args) {
TestServiceStub stub;
try {
stub = new TestServiceStub
("http://localhost:8080/axis2/services/TestService");
TestServiceStub.ServiceFunction obj = new TestServiceStub.ServiceFunction();
obj.setArg1("argument1");
obj.setArg2("argument2");
TestServiceStub.ServiceFunctionResponse res = stub.serviceFunction(obj);
TestServiceStub.DataBean data=new TestServiceStub.DataBean();
data=res.get_return();
String[] dataarray=data.getSarray();
System.out.println(data.getName()+dataarray[0]+dataarray[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}

  1. First we create the stub object of TestServiceStub , our class name was TestService hence TestServiceStub, thus to generalize it would be ClassNameStub
  2. Second We initialize the stub and give the service URL as parameter to constructor , in our example it will be as shown only , in general it would be "http://serverroot/axis2/services/Servicename"
  3. Then we create the object for our function ServiceFunction ,with syntax as shown in general it would be ClassNameStub.functionname(with first letter capital ) obj=new ClassNameStub.functionname(with first letter capital )()
  4. Then we set the input arguments , in general they can be set as obj.setInputargumentname(with first letter capital)(argument value)
  5. After setting all input arguments we create the response object using syntax as shown above , in general it would be ClassNameStub.Functionname(with first letter as capital) Response res = stub.Functionname(obj);
  6. After this we created the Databean object, in general this step wont come for default types , for custom types syntax would be ClassNameStub.DatatypeClassname data=new ClassNameStub.DataTypeClassName();
  7. After this we got the result into the databean using res.get_return() function
  8. After getting the result one can do anything with it
So general client code would look like as below (You need to change the blue text according to your class specifications)
package pacakagename;

public class ClassNameClient {
public static void main(String[] args) {
ClassNameStub stub;
try {
stub = new ClassNameStub
("http://serverroot/axis2/services/Servicename");
ClassNameStub.Functionname(with first letter as capital) obj = new ClassNameStub.Functionname(with first letter as capital)();
obj.setInputArgumentName(with first letter as capital)(argument1);
ClassNameStub.Functionname(with first letter as capital) Response res = stub.Functionname(obj);
ClassNameStub.DatatypeClassname data=new ClassNameStub.DataTypeClassName();
data=res.get_return();
} catch (Exception e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment