简体   繁体   English

Java,M子-在一个Validation组件上引发不同类型的异常

[英]Java, Mule - Throw different types of exceptions on one Validation component

In a flow I've designed, I have a Validation component with a custom validator that references a class, DataValidator , implementing the mule Validator interface. 在我设计的流程中,我具有一个Validation组件,该组件带有一个自定义验证器,该验证器引用实现DataValidator Validator接口的类DataValidator In the DataValidator , I validate several properties and would like to throw different kinds of exceptions (possibly custom exceptions, created by me) for each one. DataValidator ,我验证了几个属性,并希望为每个属性抛出不同种类的异常(可能是我创建的自定义异常)。 Is this possible? 这可能吗?

My understanding is that, by specifying the exceptionClass , the Validation component will only throw exceptions of that class. 我的理解是,通过指定exceptionClass ,Validation组件将仅抛出该类的异常。

There's the option to use an ExceptionFactory instead of an exceptionClass . 可以选择使用ExceptionFactory而不是exceptionClass Does using it allows throwing several types of exception? 使用它是否会引发几种类型的异常? If so, how can I use it? 如果可以,该如何使用? I checked this blog post, but didn't understand it well enough. 我检查了博客文章,但对它的理解不够。

In the case that none of this is possible, is there any way I can get the ValidationResult message in the custom validator component, so that I can use it in the message? 如果这一切都不可能,我有什么办法可以在自定义验证器组件中获取ValidationResult消息,以便可以在消息中使用它?

Yes you can throw several types of exceptions. 是的,您可以抛出几种类型的异常。 As you mentioned, you'll have to implement the DataValidator and ExceptionFactory interface and configure your component to use them. 如前所述,您必须实现DataValidatorExceptionFactory接口,并配置您的组件以使用它们。

With Studio, choose "Use Exception Factory Config" and specify the full class name you want to use. 在Studio中,选择“使用例外工厂配置”,然后指定要使用的完整类名。 With XML, specify the exception-factory you implemented in your validation-config . 使用XML,指定您在validation-config实现的exception-factory (You can also configure a Spring Bean and reference it): (您还可以配置Spring Bean并引用它):

    <validation:config name="Validation_Configuration" doc:name="Validation Configuration">
        <validation:exception-factory class="com.mycomp.validation.MyExceptionFactory"/>
    </validation:config>

In your flow configure a custom-validator and reference your configuration with exception factory, your validator implementation and the type of exception you want to throw with exceptionClass . 在您的流程中,配置一个custom-validator并使用异常工厂,验证器实现以及您要通过exceptionClass引发的exceptionClass类型引用您的配置。 To be able to throw any kind of Exception, specify java.lang.Exception or a class from which the custom exceptions you'll want to use can inherit: 为了能够抛出任何类型的Exception,请指定java.lang.Exception或要从中使用的自定义异常可以继承的类:

<flow name="validation-exceptionFlow">
    ...
    <validation:custom-validator
        config-ref="Validation_Configuration" 
        class="com.mycomp.validation.MyValidator"
        exceptionClass="java.lang.Exception" 
        doc:name="Validation" />
    ...
</flow>

Depending on your need you may want to specify exceptionClass differently, the idea being that your actual exceptions should extend it. 根据您的需要,您可能希望以不同的方式指定exceptionClass ,即您的实际异常应该扩展它。

Yout ExceptionFactory implementation is then up to you. 然后由您自己决定ExceptionFactory实现。 You can return whatever exception you want... For example: 您可以返回任何想要的异常...例如:

public class MyExceptionFactory implements ExceptionFactory{

    @Override
    public <T extends Exception> T createException(ValidationResult result, Class<T> exceptionClass, MuleEvent event) {
        return (T) createException(result, exceptionClass.getCanonicalName(), event);
    }

    @Override
    public Exception createException(ValidationResult result, String exceptionClassName, MuleEvent event) {
        //...
        //some logic to identify which kind of exception you want to throw
        //from result and event
        //...
        if(something) {
            return new SomeException("Something happened");
        } else if (somethingElse) {
            return new AnotherException("I am en error...");
        } else {
            return new BananaException("Ook");
        }
    }

}

It appears the interface has two methods, one returning a generic and the other a plain Exception . 看来接口有两种方法,一种返回泛型,另一种返回普通Exception Not knowing the specific usage of your ExceptionFactory I won't be able to provide much guidance, but be aware that Mule may call any of these methods, and the doc provides some requirements : 我不知道您的ExceptionFactory的具体用法,我将无法提供很多指导,但是请注意,Mule可以调用这些方法中的任何一种,并且文档提供了一些要求

The above interface receives the Event that was rejected by the validation and the validator that raised the error. 上面的接口接收被验证和引发错误的验证器拒绝的事件。 This method is intended to return the exception to be thrown but not to throw it. 此方法旨在返回要抛出的异常,但不抛出该异常。 Implementations of this interface should never throw exceptions. 此接口的实现不应抛出异常。 They should also be thread-safe and have a public default constructor. 它们还应该是线程安全的,并具有公共默认构造函数。 See Also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM