English 中文(简体)
Hibernate Validator: Intercept Invalid Values
原标题:Hibernate Validator: Intercept Invalid Values

我愿设立我的神职人员,使用Hibernate Validator(作鉴定)和谷歌古冰(供DI和拦截方法使用)。

理想的情况是,如果任何“失败”的验证方法会导致拦截方法被称作:

public class Widget {
    @NotNull
    public Fizz getFizz() {
        return fizz;
    }
}

public class FailedWidgetInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // This gets executed if Widget s getFizz() returns null...
    }
}

但如Hibernate Validator,只有允许你通过明文通过标语<>T至<代码>ClassValidator<T> sget InvalidValues()。

因此,我需要一个地方来发出这样的呼吁! <>只有可行的解决办法 我可以想到的是,创造我自己的诺言(在!) 也许这样看:

@NotNull
public @interface AutoValidatingNotNull {
    // ...??
}

之后在Guice Module上:

public class WidgetModule implements Module {
    public void configure(Binder binder) {
        binder.bindInterceptor(
            any(),
            annotatedWith(AutoValidatingNotNull.class),
            new ValidatingWidgetInterceptor()
        );
    }
}

public class ValidatingWidgetInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        ClassValidator<Widget> widgetValidator = new ClassValidator<Widget>();

        InvalidValue[] badVals = widgetValidator.getInvalidValues(widget);

        if(badVals.length > 0)
            handleFailedValidationAndThrowRuntimeExceptionOrSomething();
    }
}

最后,为了修改<条码>植被(>):

@AutoValidatingNotNull
public Fizz getFizz() {
    return fizz;
}

仅限attle 工作:在拦截器invoke <>code>方法内,,我如何在widgete(我们希望确认)栏目上接手?。 是否有办法通过说明通过<代码>widget

Edit:
Doesn t look like I can pass Object into annotations (as parameters)...

第二,这是一种新生事物。 或许 不要看一下Hibernate Validator提供的对我来说是看一看的? 是否有更好的途径? 提前感谢!

最佳回答

看来,你仍在使用Hibernate Validator 3.x AP围绕ClassValidator等。

我建议将,确切地说是你描述的内容。

An example for the required glue code to integrate that API with Google Guice can be found in this project which I created a while ago on GitHub.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签