I am trying to customize the fault responses for RuntimeException
s thrown before reaching to the WebMethod
, like invalid xml body or wrong method name for a JAX-WS service.
I tried to use a handler with @HandlerChain
but the response is sent before processed by the handler.
Here is an example. Lets say i have a method copyTool method in my ws but client sent below request. I want to be able to customize the response so that i can change the faultString or return some custom faultCode.
Input
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:cor="http://acme.com/">
<soapenv:Header/>
<soapenv:Body>
<cor:copyTool>
<toolNumber>some Text instead of Integer</toolNumber>
</cor:copyTool>
</soapenv:Body>
</soapenv:Envelope>
Output
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns0:Fault xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3.org/2003/05/soap-envelope">
<faultcode>ns0:Server</faultcode>
<faultstring>Exception Description: The object [some Text instead of Integer], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[toolNumber-->toolNumber/text()]] with descriptor [XMLDescriptor(com.acme --> [DatabaseTable(ns0:copyTool)])], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "some Text instead of Integer"</faultstring>
</ns0:Fault>
</S:Body>
</S:Envelope>
I think you can try with an ExceptionMapper as follows: