Axis2 web service framework offers number of development approaches to match various user requirements instead of limiting to support one or two specifications. This is a one distinct feature that Axis2 go beyond from some other web service frameworks. If someone want to deploy a web service very quickly using POJOs without touching XML or SOAP, AXIS2 ADB is the ideal solution for him. ADB Object support is a one of the great feature and we have done some nice set of recent improvements too. I will discuss few tips within this post that users should know in order to get best use of Axis2 ADB Object support.
Public Object objectService(Object obj){
System.out.println(obj)
return obj;
}
Since above method takes Object type as an argument and return type it should possible to send any java primitive or POJO as a parameter value. As an example for a sample String massage it is possible to have following payload.
<ns2:obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:type="xs:string">
Hello World
</ns2:obj>
In case of POJO message it is possible to have following payload.
<ns2:obj xmlns:ns1="http://sample/xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ns1:Student">
<ns1:age>20</ns1:age>
<ns1:id>200</ns1:id>
<ns1:name>saman</ns1:name>
</ns2:obj>
In above messages “xsi:type” attribute carries type system information that is required in marshalling and unmarshalling. When you send a POJO value server need to aware with the schema information associate with that particular POJO type otherwise marshalling/unmarshalling process will not possible. Typically Axis2 run-time only aware with the types associate with a particular service method, if you send above second message you will get an exception something similar to below because server can’t identify type system associate with incoming message.
Exception in thread "main" org.apache.axis2.AxisFault: Unknow type {http://sample/xsd}Student
the solution here is use a parameter called “extraClass” within your services.xml file.
e.g -
<parameter name="extraClass">sample.Item,sample.Student</parameter>
The above parameter will instruct run-time to register type details of give classes and generate schema as well . Without schema generation it is not possible to write WS clients that capable of send and receive messages with above POJOs. That is what you all need in server side now you can test your service using a tool like SoapUI.
In client side easiest and quickest way to write a client is generate code using WSDL2JAVA tool , again by default WSDL2JAVA tool also ignore extra sachem type available on WSDL file when generating codes. But it is possible to change this behaviour by adding option
“-g” that instruct to generate codes for all the schema types available . You can use following command to generate client side codes for sample application .
“-g” that instruct to generate codes for all the schema types available . You can use following command to generate client side codes for sample application .
axis2-1.7.0-SNAPSHOT/bin/wsdl2java.sh -uri http://localhost:8080/services/SimpleObjectService?wsdl -g
Now you have generated POJOs in your client side you can use them with your Object service as follows.
Student student = new Student();
student.setName("saman");
student.setId(200);
student.setAge(20);
req.setObj(student);
ObjectServiceResponse res = stub.objectService(req );
System.out.println(res.get_return());
0 comments:
Post a Comment