How do I make a service that throws a service-specific exception, and make a
java client that can catch that exception?
I wrote a very simple test case (see below). The fault transmitted on the
wire seems to be OK (see response, captured by TCPMon, at the end of this
message), but the axis client code does not throw my service specific
exception - it only throws an AxisFault.
Has anyone been able to do this? If so, how?
The details:
I am using Tomcat 4.0.4, Axis Beta 3 on Windows XP. I compiled the
interface, then ran java2wsdl to get the wsdl, then wsdl2java to get the
client stub/service/servicelocator classes.
The service is very simple:
public interface BobH extends java.rmi.Remote {
public void throwException()
throws BobHException,java.rmi.RemoteException;
}
and
public class BobHSoapBindingImpl implements BobH {
public void throwException() throws BobHException {
throw new BobHException("psycho monkey");
}
}
and
public class BobHException extends Exception {
public BobHException (String msg) {
super(msg);
}
}
On the client side:
public class BobHExceptionTest
{
public static void main(String[] args)
{
try {
BobH b = new BobHServiceLocator().getBobH();
b.throwException();
} catch (BobHException be) {
System.out.println("got it, msg is " + be.getMessage());
System.out.println("exception class name is " +
be.getClass().getName());
} catch (Exception e) {
System.out.println("Didn't get it");
System.out.println("exception class name is " +
e.getClass().getName());
}
}
}
Running this, I get:
- Mapping Exception to AxisFault
AxisFault
faultCode: {http://xml.apache.org/axis/}Server.generalException
faultString: psycho monkey
faultActor: null
faultDetail:
exceptionName: com.cotagesoft.test.webservices.bobh.BobHException
stackTrace: AxisFault
faultCode: {http://xml.apache.org/axis/}Server.generalException
faultString: psycho monkey
faultActor: null
faultDetail:
exceptionName: com.cotagesoft.test.webservices.bobh.BobHException
psycho monkey
at
com.cotagesoft.test.webservices.bobh.BobHSoapBindingImpl.throwExcepti
on(BobHSoapBindingImpl.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
### rest of stack trace omitted ###
psycho monkey
at
org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.
java:135)
### more omitted stack trace ###
at org.apache.axis.client.Call.invoke(Call.java:1251)
at
com.cotagesoft.test.webservices.bobh.BobHSoapBindingStub.throwExcepti
on(BobHSoapBindingStub.java:121)
at
com.cotagesoft.test.webservices.bobh.BobHExceptionTest.main(BobHExcep
tionTest.java:27)
Didn't get it
exception class name is org.apache.axis.AxisFault
The generated WSDL is:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
targetNamespace="http://bobh.webservices.test.cotagesoft.com"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:intf="http://bobh.webservices.test.cotagesoft.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:impl="http://bobh.webservices.test.cotagesoft.com-impl"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://bobh.webservices.test.cotagesoft.com">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexType name="BobHException">
<sequence/>
</complexType>
<element name="BobHException" nillable="true" type="intf:BobHException"/>
</schema>
</wsdl:types>
<wsdl:message name="throwExceptionRequest">
</wsdl:message>
<wsdl:message name="BobHException">
<wsdl:part name="fault" type="intf:BobHException"/>
</wsdl:message>
<wsdl:message name="throwExceptionResponse">
</wsdl:message>
<wsdl:portType name="BobH">
<wsdl:operation name="throwException">
<wsdl:input name="throwExceptionRequest"
message="intf:throwExceptionRequest"/>
<wsdl:output name="throwExceptionResponse"
message="intf:throwExceptionResponse"/>
<wsdl:fault name="BobHException" message="intf:BobHException"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BobHSoapBinding" type="intf:BobH">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="throwException">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="throwExceptionRequest">
<wsdlsoap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://bobh.webservices.test.cotagesoft.com"/>
</wsdl:input>
<wsdl:output name="throwExceptionResponse">
<wsdlsoap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://bobh.webservices.test.cotagesoft.com"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BobHService">
<wsdl:port name="BobH" binding="intf:BobHSoapBinding">
<wsdlsoap:address
location="http://localhost:8080/bobh/services/BobH"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
The response, captured by TCPMon, is:
HTTP/1.1 500 Internal Server Error
Content-Type: text/xml; charset=utf-8
Connection: close
Date: Tue, 23 Jul 2002 22:54:56 GMT
Server: Apache Tomcat/4.0.4 (HTTP/1.1 Connector)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode
xmlns:ns1="http://xml.apache.org/axis/">ns1:Server.generalException</faultco
de>
<faultstring>psycho monkey</faultstring>
<detail>
<ns2:exceptionName
xmlns:ns2="http://xml.apache.org/axis/">com.cotagesoft.test.webservices.bobh
.BobHException</ns2:exceptionName>
<ns3:stackTrace xmlns:ns3="http://xml.apache.org/axis/">AxisFault
faultCode: {http://xml.apache.org/axis/}Server.generalException
faultString: psycho monkey
faultActor: null
faultDetail: 
exceptionName: com.cotagesoft.test.webservices.bobh.BobHException

psycho monkey
at
com.cotagesoft.test.webservices.bobh.BobHSoapBindingImpl.throwException(BobH
SoapBindingImpl.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
### more omitted stack trace ### at
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125
)
at java.lang.Thread.run(Thread.java:536)
</ns3:stackTrace>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
----------
Robert Herold
Cotagesoft, Inc.
650 474 9013 x808