使用net.sf.oval进行参数验证(在运行框架中) [英] parameter validation with net.sf.oval (in play framework)

查看:1165
本文介绍了使用net.sf.oval进行参数验证(在运行框架中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为自己的方法使用@NotNull批注(或@Required或其他).尽管这在Controller和Model类中效果很好,但我无法自己使用它.这可能更多是一个net.sf.oval问题,然后是播放框架.但这可能是连接的,我不知道.

I would love to use the @NotNull annotation (or @Required or anything) for my own methods. While this works quite well in Controller and Model classes I cant get it to work in my own. This probably is more of a net.sf.oval question then play framework. But it might be connected, I don't know.

我有一个像这样的课程:

I have a class like:

@net.sf.oval.guard.Guarded
public class SimulatorWrapper {

    public SimulatorWrapper setRedCode(@play.data.validation.Required @net.sf.oval.constraint.NotNull final String redCode) {
        // just gessing here:
        if(Validation.hasErrors()) throw new RuntimeException("invalid argument");
        if(redCode == null) throw new RuntimeException("null");
        // do stuff
        return this;
    }
}

当我使用null参数调用此方法时,if引发我的异常,但是@NotNull和@Required似乎什么都没做.我究竟做错了什么? Play框架项目带有椭圆形1.5,我下载了1.8并将其添加到eclipse中的类路径中,以防旧版本出现问题.

When I call this method with a null parameter the if throws my exception, but @NotNull and @Required seem to do nothing at all. What am I doing wrong? The play framework project came with oval 1.5, I downloaded 1.8 and added it to the classpath in eclipse just in case the old one had problems.

我先用"play test my-server"启动服务器,然后用浏览器导航到我的网站(尚未测试,只是简单的网站).

I'm starting the server with "play test my-server" and then I navigate to my website (not a test yet, just simple site) with my browser.

谢谢亚历克斯

P.S.我知道"null是邪恶的"讨论,但是我无权访问其余代码,因此无法更改.

P.S. I know the "null is evil" discussion, but I dont have access to the rest of the code so I cant change that.

推荐答案

仅当调用控制器动作时,才通过Play框架调用验证类以检查验证注释.

The validation class is invoked to check the validation annotations by the Play framework only when a controller action is called.

由于您不在控制器中,因此不会执行对注解的验证,并且必填的注解也不会位于Validation.hasErrors()

Since you're not in a controller, the Validation on annotation won't be executed and the Required annotion won't be in Validation.hasErrors()

您可以使用类似以下的方法来代替使用注释: Validation.required(redCode); //它将检查是否为空 然后,调用Validation.hasErrors(),它应该可以工作.

Instead of using annotation, you could use methods like: Validation.required(redCode); //It'll check for null And after that, call Validation.hasErrors() and it should work.

但是,我不认为您应该执行此操作,因为Validation.hasError()的错误应该来自控制器操作调用的Validation,并且可能导致副作用.

However, I don't think you should do this because the errors from Validation.hasError() should come from Validation on the controller action invocation and it can cause you side effects.

如果您想执行类似示例的操作,则不应依赖play Validation类.

If you want to do something like your example, you should not rely on the play Validation class.

确定要在正确的地方使用验证吗?

Are you sure you're using validation at the right places ?

这篇关于使用net.sf.oval进行参数验证(在运行框架中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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