Spring Data Rest - 在保存之前验证 Bean

发布于 2025-01-18 09:56:05 字数 1237 浏览 1 评论 0原文

我一直在寻找最简单,最佳的方法来验证我的实体在创建/更新之前,经过大量谷歌搜索,我找不到干净/现代的。

理想情况下,我希望能够使用@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 技术交流群。

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

发布评论

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

评论(2

遗弃M 2025-01-25 09:56:05

我在链接的 GitHub 项目中检查了你的 pom.xml 。您只依赖于验证注释,但 Spring Boot 的正确方法是使用 spring-boot-starter-validation 依赖项。 Spring Boot Starter 依赖项为您的项目添加了“魔力”,它会根据您的注释自动触发验证。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

我在我的德国博客上写了一篇关于这个主题的文章:
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.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

I my German blog I have written an article about this topic:
https://agile-coding.blogspot.com/2020/11/validation-with-spring.html

走野 2025-01-25 09:56:05

我想建议每个初级/初学者都应该了解的最佳实践。不要在实体中放置任何验证注释,而是在 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.

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