How to add information to a SOAP fault message with EJB 3 based web services
Join the DZone community and get the full member experience.
Join For FreeAre you building a Java web service based on EJB3?
Do you need to return a more significant message to your web service clients other that just the exception message or even worst the recurring javax.transaction.TransactionRolledbackException?
Well if the answer is YES to the above questions then keep reading...
The code in this article has been tested with JBoss 5.1.0 but it should (!?) work on other EJB containers as well
Do you need to return a more significant message to your web service clients other that just the exception message or even worst the recurring javax.transaction.TransactionRolledbackException?
Well if the answer is YES to the above questions then keep reading...
The code in this article has been tested with JBoss 5.1.0 but it should (!?) work on other EJB containers as well
- Create a base application exception that will be extended by all the other exception, I will refer to it as MyApplicationBaseException .
This exception contains a list of UserMessage, again a class I created with some messages and locale information - You need to create a javax.xml.ws.handler.soap.SOAPHandler < SOAPMessageContext > implementation. Mine looks like this
- Now that you have the exception handler you need to register this
SoapHandler with the EJB. To do that you'll need to create an Xml file
in your class path and add an annotation to the EJB implementation
class.
The xml file :
and the EJB with annotation will be
import javax.jws.HandlerChain; @Local(MyService.class) @Stateless @HandlerChain(file = "soapHandler.xml") @Interceptors( { MyApplicationInterceptor.class }) @SOAPBinding(style = SOAPBinding.Style.RPC) @WebService(endpointInterface = "com.mycompany.services.myservice", targetNamespace = "http://myservice.services.mycompany.com") public final class MyServiceImpl implements MyService { // service implementation }
To make sure all my exceptions have proper messages and that the exception is set in the SOAPMessageContext I use an Interceptor to wrap all the service methods and transform any exception to an instance of MyApplicationException
The interceptor has a single method
That's it! You're done.
From http://www.devinprogress.info/2011/02/how-to-add-information-to-soap-fault.html
Web Service
SOAP
Web Protocols
Fault (technology)
Opinions expressed by DZone contributors are their own.
Comments