问题
I am trying to add custom error handling for Spring SOAP web services (version: 3.0.8) as per the steps in the below ticket.
How to return custom SOAP Error from Spring Boot Endpoint Service?
Added custom payload validator and EndpointExceptionResolver classes. However when the custom exception is thrown in Payload validator, it is being handled by default resolver (DetailSoapFaultResolver) instead of custom one.
Though spring is recognising the new resolver it is given low precedence. How can I set the precedence so that Custom resolver is picked by the framework. Below are more details. Please help.
Below is the order of resolvers at run time:
0 = SoapFaultAnnotationExceptionResolver
1 = DetailSoapFaultResolver
2 = CustomizedSoapFaultDefinitionExceptionResolver
3 = SimpleSoapExceptionResolver
Custom PayloadValidator:
public class CustomValidatingInterceptor extends PayloadValidatingInterceptor {
@SneakyThrows
@Override
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) {
// if any validation errors, convert them to a string and throw on as Exception to be handled by CustomSoapErrorMessageDispatcherServlet
String validationErrorsString = "error message"
throw new CustomSoapValidationException("<![CDATA[ --" + validationErrorsString + "]]");
}
return true;
}
Custom EndpointExceptionResolver:
@Component
public class CustomizedSoapFaultDefinitionExceptionResolver implements EndpointExceptionResolver {
public boolean resolveException(MessageContext messageContext, Object endpoint, Exception ex) {
if (ex instanceof CustomSoapValidationException) {
throw (CustomSoapValidationException) ex;
}
return false;
}
}
回答1:
I hit this issue today as well ...
org.springframework.ws.server.MessageDispatcher is using OrderComparator to sort EndpointExceptionResolvers in initEndpointExceptionResolvers( ... ).
OrderComparator works off PriorityOrdered interface.
If you implement PriorityOrdered to set precedence (e.g. HIGHEST_PRECEDENCE), that seems to work with influencing order.
e.g.
@Order(HIGHEST_PRECEDENCE)
public class YourEndpointExceptionResolver extends AbstractEndpointExceptionResolver implements PriorityOrdered {
...
}
来源:https://stackoverflow.com/questions/64595770/how-to-set-the-precedence-for-custom-endpointexceptionresolver-in-spring-web-ser