Search code examples
javaspringspring-bootsoapfault

How to map an exception thrown by a spring boot web service to a complex fault info?


I implement a service provider for an existing WSDL with Spring Boot. The WSDL specifies a service with an additional fault message. The corresponing fault info has some details including a timestamp, the class of the causing exception and its stack trace as well as information contained in the original request. All of the details are defined as XML elements on its own.

These information are available at runtime when the service is executed at the server. If an error occurs then an appropriate exception is thrown which contains these information.

With spring boot one can configure an instance of org.springframework.ws.server.EndpointExceptionResolver to map exceptions to fault infos. However, it seems that in all its implementing classes it is only possible to add a fault info message and a fault code. I did not find a way to add a structured object or better: an object for which a JAXB serialization is defined.

How is this possible?


Solution

  • Sure that's not a problem.

    One way could be to create a custom SoapFaultMappingExceptionResolver that maps the exception to the fault:

    public class DetailSoapFaultDefinitionExceptionResolver extends SoapFaultMappingExceptionResolver {
    
        private static final QName CODE = new QName("code");
        private static final QName DESCRIPTION = new QName("description");
    
        @Override
        protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
            logger.warn("Exception processed ", ex);
            if (ex instanceof ServiceFaultException) {
                ServiceFault serviceFault = ((ServiceFaultException) ex).getServiceFault();
                SoapFaultDetail detail = fault.addFaultDetail();
                detail.addFaultDetailElement(CODE).addText(serviceFault.getCode());
                detail.addFaultDetailElement(DESCRIPTION).addText(serviceFault.getDescription());
            }
        }
    
    }
    

    Please find a complete example here:

    https://memorynotfound.com/spring-ws-add-detail-soapfault-exception-handling/