使用 net.sf.oval 进行参数验证(在 play 框架中)

发布于 2024-12-22 10:20:42 字数 855 浏览 0 评论 0原文

我很想在我自己的方法中使用 @NotNull 注释(或 @Required 或任何东西)。虽然这在控制器和模型类中工作得很好,但我无法让它在我自己的类中工作。这可能更像是一个 net.sf.oval 问题,然后是 play 框架。但可能有联系,我不知道。

我有一个这样的类:

@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框架项目附带了oval 1.5,我下载了1.8并将其添加到eclipse中的类路径中,以防旧的出现问题。

我使用“play test my-server”启动服务器,然后使用浏览器导航到我的网站(还不是测试,只是简单的网站)。

谢谢,亚历

克斯我知道“空是邪恶的”讨论,但我无权访问其余的代码,所以我无法更改它。

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;
    }
}

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.

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.

Thanks, Alex

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-12-29 10:20:42

仅当调用控制器操作时,Play 框架才会调用验证类来检查验证注释。

由于您不在控制器中,因此不会执行注释上的验证,并且所需的注释不会出现在 Validation.hasErrors() 中

,您可以使用以下方法而不是使用注释:
验证.required(redCode); //它会检查是否为空
之后,调用 Validation.hasErrors() 它应该可以工作。

但是,我认为您不应该这样做,因为 Validation.hasError() 中的错误应该来自控制器操作调用上的验证,并且它可能会导致副作用。

如果您想做类似示例的操作,则不应依赖 play Validation 类。

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

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

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()

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.

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.

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 ?

北城孤痞 2024-12-29 10:20:42

万一有人还需要这个。

您可以在所有类中使用 Play 注释进行验证,方式与在控制器中完全相同。
只需使用 validate 插件即可。

In case anyone still needs this.

You can do validation with Play annotations in all classes exactly the same way as in controllers.
Just use validate plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文