Thursday, May 20, 2010

AXIS2 web service client - without sub/proxy ...

This article is regarding how to call an web service without modifying the client each time for diff. web service OR method call. Basically we use stub/proxy classes for diff. web service method calls using Axis2, but internelly it calls using XML SOAP envelopes. So, we can direcftly convert the client to use Axis2 API classes and provide an implementation which only needs to handle the response from the call. Response will be in XML format,so we need to fetch data from XML. We can covnert XML into Pojos using Castor or JAXB libs.

You can directly use ServiceClient class to call web service, which provides call using XML only and returns XML response. For different methods of web service, you have to convert the XML response to some java POJO to use it. Only Response handling needs to be done at your end. that you can do like from XML to Map etc...

So you won't need any other stub classes to call any web service, only needs to handle response XML or converted POJO.

This is the way you don't need to modify your client every time for diff. web services. You can develop like providing a response handler to client externally. So that for every different web service you will have diff. response handler class which is implementation of you interface.

//common interface for response handlers...
//implement this for diff. web service/methods

public interface WSRespHandler{
    public Object getMeResp(Object respData);
}

//pass particular handler to client when you call some WS
public class WebServiceClient {
    public Object getResp(WSRespHandler respHandler) {
        ...
        return repHandler.getMeResp(xmlData);
    }
}

References:

http://www.developer.com/java/web/article.php/3863416/Using-Axis2-and-Java-for-Asynchronous-Web-Service-Invocation-on-the-Client-Side.htm

http://www.devdaily.com/blog/post/java/java-web-service-client-read-array-list/

0 Comments: