From 1afb1386a54dbb261db79065ac8e0353b0307951 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 20 Dec 2015 19:26:37 +0100 Subject: [PATCH] jaxb2: improvements in MyCustomMappingExceptionResolver --- .../MyCustomMappingExceptionResolver.java | 41 +++++++++++++--------- .../exceptions/CustomBusinessException.java | 2 +- .../impl/CustomBindingExampleServiceImpl.java | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/MyCustomMappingExceptionResolver.java b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/MyCustomMappingExceptionResolver.java index f2a375f..f07d42e 100644 --- a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/MyCustomMappingExceptionResolver.java +++ b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/MyCustomMappingExceptionResolver.java @@ -23,8 +23,8 @@ import de.spring.webservices.auto.Element; import de.spring.webservices.auto.ExampleFault; import de.spring.webservices.exceptions.CustomBusinessException; -public class MyCustomExceptionResolver extends AbstractSoapFaultDefinitionExceptionResolver { - private static final Logger LOGGER = LoggerFactory.getLogger(MyCustomExceptionResolver.class); +public class MyCustomMappingExceptionResolver extends AbstractSoapFaultDefinitionExceptionResolver { + private static final Logger LOGGER = LoggerFactory.getLogger(MyCustomMappingExceptionResolver.class); private Marshaller marshaller; @@ -94,35 +94,44 @@ public class MyCustomExceptionResolver extends AbstractSoapFaultDefinitionExcept } protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) { - SoapFaultDetail detail = fault.addFaultDetail(); - Result result = detail.getResult(); - - Element element = new Element(); - element.setMessage(ex.getMessage()); - - if (ex instanceof CustomBusinessException) { - List arguments = element.getMessageArgs(); - arguments.add("ARGUMENT 1"); - arguments.add("ARGUMENT 2"); - } - ExampleFault customFault = new ExampleFault(); customFault.setTechnicalError(getStackTrace(ex)); + + Element element = buildElement(ex); List elements = customFault.getElements(); elements.add(element); + SoapFaultDetail detail = fault.addFaultDetail(); + Result result = detail.getResult(); try { marshaller.marshal(customFault, result); } catch (Exception marshallEx) { - LOGGER.error("", marshallEx); + LOGGER.error("MyCustomMappingExceptionResolver: marshaller error", marshallEx); } } public void setMarshaller(Marshaller marshaller) { this.marshaller = marshaller; } + + private Element buildElement(Exception ex) { + Element element = new Element(); + element.setMessage(ex.getMessage()); + + if (ex instanceof CustomBusinessException) { + CustomBusinessException customEx = (CustomBusinessException) ex; + List messageArgs = element.getMessageArgs(); + List argumentsEx = customEx.getArguments(); + + for (String argumentEx: argumentsEx) { + messageArgs.add(argumentEx); + } + } + + return element; + } - private static String getStackTrace(final Throwable throwable) { + private String getStackTrace(Throwable throwable) { final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); diff --git a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/exceptions/CustomBusinessException.java b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/exceptions/CustomBusinessException.java index 952e765..549c0b9 100644 --- a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/exceptions/CustomBusinessException.java +++ b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/exceptions/CustomBusinessException.java @@ -3,7 +3,7 @@ package de.spring.webservices.exceptions; import java.util.List; /** - * This exception will be caught by de.spring.webservices.endpoints.MyCustomExceptionResolver + * This exception will be caught by de.spring.webservices.endpoints.MyCustomMappingExceptionResolver * */ public class CustomBusinessException extends RuntimeException { diff --git a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java index c5ce222..9863bae 100644 --- a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java +++ b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java @@ -28,7 +28,7 @@ public class CustomBindingExampleServiceImpl implements // see soap-ws.xml Spring configuration file. // throw new BusinessException("This feature has not been implemented yet."); - // Example about how works de.spring.webservices.endpoints.MyCustomExceptionResolver + // Example about how works de.spring.webservices.endpoints.MyCustomMappingExceptionResolver // see soap-ws.xml Spring configuration file. // List arguments = new ArrayList<>(); // arguments.add("ARGUMENT 1"); -- 2.1.4