This section gives a general method to write a Java Class for any Web Service. This method is adopted by me and to be sure I dont know if its the best method , anyway everybody is free to use their style of coding the class. The JavaToWSDL tool does not care how you are getting the work done , it just concerns about the datatypes your service is returning and the datatypes its taking as input.
Each datatype used for returning or taken as input by the service should be a JavaBean if the datatype is custom made class , default types of java are off coarse allowed :).
We will be using Eclipse and We will create a package let name it webservice1
- First create a new Java Project let name the project LearningWebService in Eclipse .
- Second, Create a DataBean class in the project which we will use as return type . One can put any datatypes in databean and then send the whole databean via service . The databean code is as follows
package webservice1 ;
import java.io.Serializable;
public class DataBean implements Serializable {
/*Put the variables which you want to
send here */
String name;
String[] sarray;
public DataBean() {}
public String getName()
{
return name;
}
public String getSarray()
{
return sarray;
}
public String setName(String name)
{
this.name=name;
}
public void setSarray(String[] sarray)
{
this.sarray=sarray;
}
}
- Now we will write the Java service class.This should be in seperate TestService.java file
package webservice1 ;public class TestService
{
public DataBean serviceFunction(String arg1,String arg2)
// Assuming we have two input arguments
{
/*do processing with input parameters using
any java classes u want.
Let say we get a result after processing*/
String[] resultarray={"Test"," Success"};
String name="Chirag";
DataBean data=new DataBean();
data.setName(name);
data.setSarray(resultarray);
return data;
}
}
- Now create a dummy class containing a main function just to compile the above classes formed. Dummy class is as follow.
public class dummy
{
public static void main(String[] argv)
{
TestService temp=new TestService();
return;
}
}
I believe we got the .class file in the respective folder in project directory.
Now lets move to next step of creating WSDL out of this Java class file.