Monday, January 3, 2011

Develop Web Service With Axis2 #3 - Custom Fault Message

Prev >>> Develop Web Service With Axis2 #2 - Work on Skeleton Code

public interface UserServices {
    UserInfoResp getUserInfo(String userId) throws WebServiceFault;

    UserInfoResp authUser(AuthUserReq authReq)  throws WebServiceFault;
}

Take note:

All the methods in the interface class,which will be exposed to web service, throw WebServiceFault.

// ==== WebServiceFault.java =====
package com.test.axis.bean;

public class WebServiceFault extends Exception {
    private String errCode;
    private String errMessage;

     // setter and getter methods
}

I want to respond an error code and an error description to web service consumer if something is wrong.

---- wsdl

<wsdl:operation name="getUserInfo">
            <soap:operation soapAction="urn:getUserInfo" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="WebServiceFault">
                <soap:fault use="literal" name="WebServiceFault"/>
            </wsdl:fault>
        </wsdl:operation>

---- skeleton code

    public com.test.axis.ws.bean.GetUserInfoResp getUserInfo(
            com.test.axis.ws.bean.GetUserInfo getUserInfo2)
            throws WebServiceFault {
        if (getUserInfo2== null || getUserInfo2.getUserId() == null) {
            WebServiceFault fault = new WebServiceFault();
            com.test.axis.ws.bean.WebServiceFaultE wsFaultE = new com.test.axis.ws.bean.WebServiceFaultE();
            com.test.axis.ws.bean.xsd.WebServiceFault param = new com.test.axis.ws.bean.xsd.WebServiceFault();
            param.setErrCode("Error Code 9999");
            param.setErrMessage("Error Desc: parameter is Null.");

            wsFaultE.setWebServiceFault(param);
            fault.setFaultMessage(wsFaultE);
            throw fault;
        }
        return null;
    }

---- request soap

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://axis.test.com/ws" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
  <q0:getUserInfo />
</soapenv:Body>
</soapenv:Envelope>

in this case, the request parameter is empty

--- response soap

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
  <soapenv:Fault>
  <faultcode>soapenv:Server</faultcode>
  <faultstring>WebServiceFault</faultstring>
  <detail>
  <ns2:WebServiceFault xmlns:ns2="http://axis.test.com/ws">
  <ns2:WebServiceFault xmlns:ns1="http://bean.axis.test.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:WebServiceFault">
  <ns1:errCode>Error Code 9999</ns1:errCode>
  <ns1:errMessage>Error Desc: parameter is Null.</ns1:errMessage>
  </ns2:WebServiceFault>
  </ns2:WebServiceFault>
  </detail>
  </soapenv:Fault>
  </soapenv:Body>
  </soapenv:Envelope>

from the elements in blue, we can see error code and message returned successfully.

----- stub code


try {
    _stub.getUserInfo(new GetUserInfo());
    } catch (RemoteException e) {
    e.printStackTrace();
    } catch (WebServiceFault e) {
    // print the custom error code and error message
    System.out.println(e.getFaultMessage().getWebServiceFault().getErrCode());
    System.out.println(e.getFaultMessage().getWebServiceFault().getErrMessage());
    }

------------------------------------------------------------

TODO:

a> consider to use the following elements to customize fault message

  <faultcode>ABC</faultcode>
  <faultstring>XYZ</faultstring>
  <detail>Detail of Fault</detail>

I have not tried it out.

probably, the following reference websites will be helpful:
http://www.cnblogs.com/huqingyu/archive/2008/04/09/1145868.html
http://apps.hi.baidu.com/share/detail/23219236
http://hi.baidu.com/hero%CD%F5%E6%DD/blog/item/ad3b47107dc024c1f6039eb7.html

No comments:

Post a Comment