I assume you have an endpoint similar to the one below:
import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.soap.SoapFaultException; import org.w3c.dom.Element; @Endpoint public class HolidayEndpoint { private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas"; public HolidayEndpoint() { } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest") public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
Now, let's say handleHolidayRequest () is where you expect a RuntimeException to occur, you will need to change the code as follows:
import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.soap.SoapFaultException; import org.w3c.dom.Element; @Endpoint public class HolidayEndpoint { private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas"; public HolidayEndpoint() { } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest") public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception { try {
Notice how I catch the runtime exception and throw a new SoapFaultException? This does the trick, and the answer is:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Server</faultcode> <faultstring xml:lang="en">Something wrong on our end. Try again later.</faultstring> </SOAP-ENV:Fault> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
Now, if you use something like spring integration with a service activator, this is just the same concept, but instead of throwing a SoapFaultException, you just collect the message with SoapFaultException as a payload:
@ServiceActivator(inputChannel = "input", outputChannel = "output") public Message<?> handleHolidayRequest(HolidayRequest holidayRequest) { try { // your code to handle the request on the server } catch (RuntimeException e) { String faultMessage = "Something wrong on our end. Try again later."; SoapFaultException soapFaultException = new SoapFaultException(faultMessage); return MessageBuilder.withPayload(soapFaultException).build(); } }
PS: For your understanding, I used the training web service example to illustrate (and I have a working example if you are still struggling): http://docs.spring.io/spring-ws/site/reference/html/tutorial .html
Good luck
source share