I am creating a web service using JAX-WS (I am creating this using the Java to WSDL approach).
Im having trouble getting my exception to work as I require.
I have created the following exception class:
@WebFault
public class MyWebServiceException extends SOAPFaultException {
private static final long serialVersionUID = 8234753208722614057L;
protected MyWebServiceException(SOAPFault fault) {
super(fault);
}
protected MyWebServiceException(SOAPFault fault, Throwable throwable) {
this(fault);
super.setStackTrace(throwable.getStackTrace());
}
}
This allows me to have the following in my SOAP response:
<faultcode>E0010</faultcode>
<faultstring>Invalid Report</faultstring>
<detail>
<ns2:exception class="com.example.web.common.exceptions.MyWebServiceException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false" xmlns:ns2="http://jax-ws.dev.java.net/">
<message>Invalid Report</message>
<ns2:stackTrace>
<ns2:frame class="com.example.web.common.exceptions.ExceptionHandler" file="ExceptionHandler.java" line="34" method="getWebException"/>
However, because my exception is a SOAPFaultException
which extends RuntimeException
, it is not being added to the WSDL, so when users of the service generate their client stubs the exception is not included.
Does anyone know how to create this exception to give me the intended output (ie including the faultcode and faultstring) but also appear in the WSDL (I guess it needs to be a checked exception)
I searched SO high and low to come up with what I have already!
If you declare that your WebMethod throws MyWebServiceException, it will appear on WSDL.
But probably you should not mess with SOAPFault codes. If you create a business exception and throw it, you can get error codes in client withoud messing with soap internals.
Your webmethod would look like:
Your business exception:
And the return onexception will be:
You can also add a stack trace but that would be better to get logged in server, maybe with a GUUID to correlate with client error if needed.
Assuming You are using JAX-WS and not JAX-RPC you could just remove the
extends SOAPFaultException
. Afterwards add the required fields to your own exception:This is a StackTraceElement (just as an example):
This is the SOAP-Info:
And this is the real exception:
I honestly didnt get around to putting this into a test project. So you might need to iron out some compile issues. But I hope you get the picture.