Java EE 参数约束配置奇怪的行为

标签 java jakarta-ee bean-validation facade

我试图在标准 AbstractFacade(由 NetBeans 生成)的创建和编辑方法上添加参数约束配置(bean 验证)。

所以我试过:

@Override
public void create(@WkTeilnahmePlanedResult  WkTeilnahme entity) {
   super.create(entity);
}

这返回了消息

A method overriding another method must not alter the parameter constraint configuration when deploying it to Glassfish 4



所以下一次尝试是
  @Override
  public void create(WkTeilnahme entity) {
    checkedCreate(entity);
  }


  private void checkedCreate(@WkTeilnahmePlanedResult WkTeilnahme entity) {
    super.create(entity);
  }

部署没有任何问题......但从未调用 validator 。

你能告诉我为什么吗?

顺便提一句:
  @Override
  public void create(WkTeilnahme entity) {
    throw new UnsupportedOperationException(
            "Create not supported! Use checkedCreate() instead!");
  }


  public void checkedCreate(@WkTeilnahmePlanedResult WkTeilnahme entity) {
    super.create(entity);
  }

这有效,但不是很酷!

最佳答案

关于您的第一次尝试,它不起作用,因为 Bean 验证约束必须遵循 Liskov Substitution Principle .另请参阅相关的 Bean 验证规范部分 - http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-methodlevelconstraints-inheritance

从规范:

Very informally speaking, the Liskov substitution principle says that where a given type T is used, it should be possible to replace T with a sub-type S of T ("Behavioral subtyping"). If S overrides/implements a method from T and S would strengthen the method's preconditions (e.g. by adding parameter constraints) this principle would be violated as client code working correctly against T might fail when working against S. Also if S overrides/implements a method from T and S weakens the method's postconditions this principle would be violated. However S may strengthen the method's postconditions (by adding return value constraints), as client code working against T still will work against S.



我认为你的第二个例子应该可以工作,但是,我不熟悉 NetBeans AbstractFacade。我的猜测是调用了 checkedCreate(entity);不通过代理实例,因此不会被拦截。也许您可以发布所涉及类的完整代码?什么类型的类包含这些方法? session bean?

关于Java EE 参数约束配置奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593452/

相关文章:

validation - 如何在 Spring Boot 中使用@NotNull?

Java MySQL Timestamp时区问题

java - 将单字符字符串追加到 StringBuilder

java - 使用 Oracle 的 Spring Boot 2.0 分页不起作用

rest - 如何关闭输入响应的 InputStream(jax.rs)

java - 是否可以在 JSF 1.2 中使用 JSF 2.0 提供的 bean 级别验证

java - 如何通过提供 POST-only @Valid 来删除冗余的 Spring MVC 方法?

java - 理解枚举的静态成员初始化

java - Thymeleaf - 如何按索引循环列表

java - 如果字符串中缺少一部分,如何设置时间戳?