a peek into my MIND

September 10, 2008

Throwing custom exception types in Axis 2

Filed under: Java — Bharat Kondeti @ 7:47 pm

After doing some search we realized that axis 2 does not throw custom exceptions in a straight forward way, it will always throw an AxisFault, unless detail is set explicitly with the exception we want to throw.

Following is an example soap fault message where detail is set to throw ServiceFault. So when ever server throws a ServiceFault, client will catch the exception as ServiceFault rather than AxisFault.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>100</faultcode>
<faultstring>Something</faultstring>
<detail>
<dtaService:serviceFault xmlns:dtaService="xxx" xmlns:types="yyy">
<types:messages>
<types:message>
<types:code>XXX</types:code>
<types:text>XXXXXX</types:text>
</types:message>
</types:messages>
</dtaService:serviceFault>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

detail in AxisFault is a OMElement, so the custom exception has to be converted into OMElement. This can be done using classes and utilities provide by axis2. Following is an example code to create AxisFault with detail set to it.

ServiceFault serviceFault = new ServiceFault("Some Fault");
XMLStreamReader reader = BeanUtil.getPullParser(serviceFault);
StreamWrapper parser = new StreamWrapper(reader);
StAXOMBuilder stAXOMBuilder =OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
OMElement element = stAXOMBuilder.getDocumentElement();
AxisFault af = new AxisFault(serviceFault.getMessage());
af.setDetail(element);

Advertisements

5 Comments »

  1. Is “ServiceFault” the custom exception class you are trying to throw ?
    Sorry..newbie to web service.

    Thanks

    Comment by CodeGazer — September 8, 2009 @ 5:18 pm

  2. yes ServiceFault is the custom exception I am trying to throw

    Comment by merereflections — September 22, 2009 @ 2:35 pm

  3. I am trying to do the same however I am still not able to catch my custom exception. After your code I can see the detail element in my SOAP response. Do you have to do any specific in your custom exception class also? Can you share the code for ServiceFault class?

    Comment by Deepak Sharma — February 3, 2010 @ 9:30 pm

  4. ServiceFault is just a custom exception that extends Exception class.

    For above code to work, the service should have been deployed in a application server. If you try to unit test the code, BeanUtil.getPullParser(serviceFault); will throw an exception.

    So I would suggest, deploy the web-service and write a client to see if you can catch the exception.

    Comment by merereflections — February 4, 2010 @ 2:34 pm

  5. This saved my ass… Thnx

    Comment by Virgiu — August 16, 2010 @ 12:17 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: