对于最新的稳定版本,请使用 Spring Data REST 4.3.1! |
对于最新的稳定版本,请使用 Spring Data REST 4.3.1! |
在Spring Data REST中注册实例有两种方法:通过bean名称连接实例或手动注册验证器。在大多数情况下,简单的 Bean 名称前缀样式就足够了。Validator
为了告诉Spring Data REST你希望将一个特定事件分配给一个特定的事件,请在bean名称前面加上相关事件。例如,要在将新实例保存到存储库中之前验证该类的实例,您可以在 ium 中声明一个 bean 名称为 的实例。由于前缀与已知的 Spring Data REST 事件匹配,因此该验证器连接到正确的事件。Validator
Person
Validator<Person>
ApplicationContext
beforeCreatePersonValidator
beforeCreate
手动分配验证者
如果您不想使用 Bean 名称前缀方法,则需要向 Bean 注册验证器的实例,该 Bean 的工作是在正确事件后调用验证器。在实现 的配置中,重写该方法并调用 ,传递要触发此验证程序的事件和验证程序的实例。以下示例演示如何执行此操作:RepositoryRestConfigurer
configureValidatingRepositoryEventListener
addValidator
ValidatingRepositoryEventListener
@Override
void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener v) {
v.addValidator("beforeSave", new BeforeSaveValidator());
}