如何在 Spring Web 服务中设置自定义 EndpointExceptionResolver 的优先级 [英] How to set the precedence for custom EndpointExceptionResolver in Spring web services

查看:36
本文介绍了如何在 Spring Web 服务中设置自定义 EndpointExceptionResolver 的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照以下票证中的步骤为 Spring SOAP Web 服务(版本:3.0.8)添加自定义错误处理.

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.

如何从Spring Boot 端点服务?

添加了自定义负载验证器和 EndpointExceptionResolver 类.但是,当在 Payload 验证器中抛出自定义异常时,它会由默认解析器 (DetailSoapFaultResolver) 而不是自定义解析器处理.

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.

虽然 spring 正在识别新的解析器,但它的优先级较低.如何设置优先级以便框架选择自定义解析器.以下是更多详细信息.请帮忙.

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

自定义 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;
  }

自定义 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;
    }
}

推荐答案

我今天也遇到了这个问题......

I hit this issue today as well ...

org.springframework.ws.server.MessageDispatcher 正在使用 OrderComparator 对 initEndpointExceptionResolvers( ... ) 中的 EndpointExceptionResolvers 进行排序.

org.springframework.ws.server.MessageDispatcher is using OrderComparator to sort EndpointExceptionResolvers in initEndpointExceptionResolvers( ... ).

OrderComparator 在 PriorityOrdered 接口下工作.

OrderComparator works off PriorityOrdered interface.

如果您实施 PriorityOrdered 来设置优先级(例如 HIGHEST_PRECEDENCE),这似乎可以影响顺序.

If you implement PriorityOrdered to set precedence (e.g. HIGHEST_PRECEDENCE), that seems to work with influencing order.

例如

@Order(HIGHEST_PRECEDENCE)
public class YourEndpointExceptionResolver extends AbstractEndpointExceptionResolver implements PriorityOrdered {
  ... 
}

这篇关于如何在 Spring Web 服务中设置自定义 EndpointExceptionResolver 的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆