如何将域对象的ID添加到验证消息中?

发布于 2025-01-28 04:24:21 字数 1865 浏览 2 评论 0原文

我有一个Spring Boot应用程序,该应用程序使用javax.validation的注释来验证我的域对象。不幸的是,错误消息不包含域对象的ID。我需要ID进行操作和开发(调试)。

代码

@SpringBootApplication
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }

  @Data
  static class Model {
    private String id;

    @Min(2)
    private int number;
  }

  @RestController
  static class Controller {

    @PostMapping("/test")
    public int test(@RequestBody @Valid Model model) {
      return 1;
    }
  }
}

logs

2022-05-12 10:39:15.252  WARN 1424 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public int test.TestApplication$Controller.test(test.TestApplication$Model): [Field error in object 'model' on field 'number': rejected value [1]; codes [Min.model.number,Min.number,Min.int,Min]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [model.number,number]; arguments []; default message [number],2]; default message [muss größer-gleich 2 sein]] ]

研究

问题

如何将域对象的ID添加到验证消息中?

I have a Spring Boot application, which uses annotations from javax.validation to validate my domain object. Unfortunately, the error message doesn't contain the ID of the domain object. I need the ID for operation and development (debugging).

Code

@SpringBootApplication
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }

  @Data
  static class Model {
    private String id;

    @Min(2)
    private int number;
  }

  @RestController
  static class Controller {

    @PostMapping("/test")
    public int test(@RequestBody @Valid Model model) {
      return 1;
    }
  }
}

Logs

2022-05-12 10:39:15.252  WARN 1424 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public int test.TestApplication$Controller.test(test.TestApplication$Model): [Field error in object 'model' on field 'number': rejected value [1]; codes [Min.model.number,Min.number,Min.int,Min]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [model.number,number]; arguments []; default message [number],2]; default message [muss größer-gleich 2 sein]] ]

Research

  • I can add values of the annotation attributes and the value of the validated property, see Hibernate Validator:

    4.1.2. Interpolation with message expressions

    As of Hibernate Validator 5 (Bean Validation 1.1) it is possible to use the Unified Expression Language (as defined by JSR 341) in constraint violation messages. This allows to define error messages based on conditional logic and also enables advanced formatting options. The validation engine makes the following objects available in the EL context:

    • the attribute values of the constraint mapped to the attribute names

    • the currently validated value (property, bean, method parameter etc.) under the name validatedValue

    • a bean mapped to the name formatter exposing the var-arg method format(String format, Object... args) which behaves like java.util.Formatter.format(String format, Object... args).

  • I can write a custom message interpolation, but the MessageInterpolator.Context only provides the validated value.

Question

How can I add domain object's ID to the validation message?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文