From b78b151aba3bf70f59d1f6e5dff9651f512ebd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Sun, 6 Apr 2025 11:32:05 +0200 Subject: [PATCH] Add example of customizing the detail of a SOAP fault Closes gh-716 --- spring-ws-docs/src/docs/asciidoc/server.adoc | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spring-ws-docs/src/docs/asciidoc/server.adoc b/spring-ws-docs/src/docs/asciidoc/server.adoc index 48a60784..49b2de51 100644 --- a/spring-ws-docs/src/docs/asciidoc/server.adoc +++ b/spring-ws-docs/src/docs/asciidoc/server.adoc @@ -1190,7 +1190,40 @@ Rather than expose the innards of your application by giving an exception and st Endpoint exception resolvers are automatically picked up by the `MessageDispatcher`, so no explicit configuration is necessary. Besides implementing the `EndpointExceptionResolver` interface, which is only a matter of implementing the `resolveException(MessageContext, endpoint, Exception)` method, you may also use one of the provided implementations. + The simplest implementation is the `SimpleSoapExceptionResolver`, which creates a SOAP 1.1 Server or SOAP 1.2 Receiver fault and uses the exception message as the fault string. +You can subclass it to customize the fault, as shown in the following example: + +[source,java] +---- +public class CustomSoapExceptionResolver extends SimpleSoapExceptionResolver { + + private final Transformer transformer; + + public CustomSoapExceptionResolver(Transformer transformer) { + this.transformer = transformer; + } + + @Override + protected void customizeFault(MessageContext messageContext, Object endpoint, Exception exception, + SoapFault fault) { + SoapFaultDetail faultDetail = fault.addFaultDetail(); + try { + this.transformer.transform(new StringSource(""" + + Your custom error code + A system message + + """), faultDetail.getResult()); + } + catch (TransformerException ex) { + throw new IllegalArgumentException("Failed to write detail", ex); + } + + } +} +---- + The `SimpleSoapExceptionResolver` is the default, but it can be overridden by explicitly adding another resolver. === `SoapFaultMappingExceptionResolver`