Spring Data Rest - 在保存之前验证 Bean
我一直在寻找最简单,最佳的方法来验证我的实体在创建/更新之前,经过大量谷歌搜索,我找不到干净/现代的。
理想情况下,我希望能够使用@Valid如下:
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Slf4j
@Validated
@Component
@RepositoryEventHandler
public class CustomerEventHandler {
// Triggered for POST
@HandleBeforeCreate
public void onBeforeCreate(@Valid Customer entity) {
log.info("Saving new entity {}", entity);
}
// Triggered for PUT / PATCH
@HandleBeforeSave
public void onBeforeSave(@Valid Customer entity) {
log.info("Saving new entity {}", entity);
}
}
客户实体是:
import javax.validation.constraints.NotBlank;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "customer")
public class Customer {
@NotBlank
private String firstname;
}
但是它似乎不起作用。
在春季数据休息中验证实体的现代,简便的方法是什么?
注意:我正在使用Spring Boot
I've been searching for the simplest and best way to validate my entities before they are created/updated and after much googling, I couldn't find a clean/modern one.
Ideally, I would have loved to be able to use @Valid as follows:
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Slf4j
@Validated
@Component
@RepositoryEventHandler
public class CustomerEventHandler {
// Triggered for POST
@HandleBeforeCreate
public void onBeforeCreate(@Valid Customer entity) {
log.info("Saving new entity {}", entity);
}
// Triggered for PUT / PATCH
@HandleBeforeSave
public void onBeforeSave(@Valid Customer entity) {
log.info("Saving new entity {}", entity);
}
}
The Customer entity being:
import javax.validation.constraints.NotBlank;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "customer")
public class Customer {
@NotBlank
private String firstname;
}
But it doesn't seem to work.
What's the modern, easy way to validate entities in Spring Data REST?
Note: I'm using Spring Boot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在链接的 GitHub 项目中检查了你的 pom.xml 。您只依赖于验证注释,但 Spring Boot 的正确方法是使用 spring-boot-starter-validation 依赖项。 Spring Boot Starter 依赖项为您的项目添加了“魔力”,它会根据您的注释自动触发验证。
我在我的德国博客上写了一篇关于这个主题的文章:
https://agile-coding.blogspot.com/2020/ 11/validation-with-spring.html
I checked your pom.xml in linked GitHub project. You have just a dependency to validation annotations, but the proper way with Spring Boot is to use the spring-boot-starter-validation Dependency. The Spring Boot Starter Dependencies add the "magic" to your project, which triggers automatically the validation based on your annotations.
I my German blog I have written an article about this topic:
https://agile-coding.blogspot.com/2020/11/validation-with-spring.html
我想建议每个初级/初学者都应该了解的最佳实践。不要在实体中放置任何验证注释,而是在 DTO/资源类中使用它们。练习验证的最佳方法是,您可以在自己的 Spring Boot 异常处理程序类中处理
MethodArgumentNotValidation
异常,注释该类@RestControllerAdvice
并创建您自己的@接口
注释而不是使用更多的验证注释。I want to suggest a few best practise that every developer, who starting as junior/beginner should be know. Don't put any Validation annotation in Entities, using them in DTO/Resource classes. and the best way to practise validation is that you can handler
MethodArgumentNotValidation
exception in your own Spring Boot of Exception Handler class annotated that class@RestControllerAdvice
and create your own@interface
annotation instead of using more validation annotation.