Spring-数据验证
我只是想知道 Spring 中的数据验证。 Spring 提供了两个(也许更多)验证选项。第一个选择是使用 Validator 接口并自行创建整个验证。第二个选项是使用注释验证(JSR 303)。
现在我真的很困惑:)我应该选择哪个验证。我需要的是检查收到的数据对象是否正确(正确是指所有必填字段均已填写),这可以通过 JSR 303 验证或我自己的验证器使用“验证器实例”来完成。但我还需要检查此数据对象是否针对某些数据库约束有效(需要验证器来检查数据库中的某些数据,例如是否注册了此 ID 的用户...)并且这只能由我自己完成验证器。
我不知道哪种方式应该是最好的。结合两者还是创建我自己的验证器?
我将感谢任何建议...
更新(从评论中重新定位)
好的,我按照 Ryan 的例子,我认为我是成功的。我创建了自己的 spring Validator 实现,并在这个 Validator 中使用 @Autowire-d javax JSR 303 实例。但注射时出现了问题。我在我的配置中,这段代码导致了一些异常,因为spring不知道我想注入哪个验证器。所以我删除了该代码。
最后我还删除了 spring Validator 实现,因为我不知道在哪里可以获取 Errors 属性,该属性是“validate”方法中第二个参数所必需的:)。我从服务层手动触发该验证,但我真的不知道在哪里可以获取该 Error 对象。
顺便说一句,我找到了另一个解决方案来实现该验证。我正在考虑通过 LocalValidatorFactoryBean 扩展我的验证器类。 LocalValidatorFactoryBean 类实现两个 Validator 接口(Spring Validator + JSR 303)。但我不确定这是否是个好方法。这种方法还需要 Error 对象,我不知道在哪里找到/获取它。
更新 Error 对象来自“BindException”。
FooObjectVO myObject = new FooObjectVO();
BindException errors = new BindException("fooObject", myObject);
I'm just wondering about data validation in Spring. Spring is offering two (maybe more) validation options. First option is to use Validator interface and create whole validation on my own. Second option is to use annotation validation (JSR 303).
And now I'm really confused :) which validation I should chose. What I need is to check if recieved Data Object is correct (by correct i mean all required fields are filled) and this can be done by JSR 303 validation or by my own validator with "Validator instance". But I also need to check if is this Data Object valid against some database constraints (validator is required to check some data in database, eq. is user with this ID registered or not ...) and this can be done only by my own validator.
I don't know which way should be the best. Combine both or create my own validator ?
I will be thankful for any advice ...
Update (relocated from comments)
Ok, I followed Ryan's example and I think I was successful. I created my own implementation of spring Validator and in this Validator I @Autowire-d javax JSR 303 instance. But there was problem with that injection. I had in my configuration and this piece of code caused some exceptions, because spring did not know which Validator I want to inject. So I removed that code.
At the end I also removed the spring Validator implementation, because I dont know where I can get Errors property, which is required as second parameter in "validate" method :). I'm triggering that validation manually from service layer and I really don't know, where I can obtain that Error object.
BTW Well, I found another solution how to implement that validation. I'm thinking about to extend my validator class by LocalValidatorFactoryBean. LocalValidatorFactoryBean class implementing both Validator interfaces (Spring Validator + JSR 303). But i'm not sure if is this good approach. This approach also require Error object, which I don't know where to find/get.
Update
The Error object is coming from "BindException".
FooObjectVO myObject = new FooObjectVO();
BindException errors = new BindException("fooObject", myObject);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们并不是真正独立的选项——更像是两个可用的验证实现。由于 Spring 的 JSR 303 支持 还实现了自己的Validator接口,您实际上不必选择其中之一。您可以以任何最容易完成工作的方式混合和匹配实现。
They're not really separate options--more like two available implementations of validation. Since Spring's JSR 303 support also implements its own Validator interface, you don't really have to pick one or the other. You can mix and match the implementations in whatever way makes it easiest to get the job done.
在这种情况下,我更喜欢将两者结合起来。我喜欢尽可能使用 JSR 303 验证。我用我自己的验证器来补充它。
In cases such as this I prefer to combine both. I like to use JSR 303 validation for whatever I can. I supplement it with my own validators.