- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Bean Validation specification定义:
Constraint annotations must define a payload element that specifies the payload with which the constraint declaration is associated. The type of the payload parameter is
Payload[]
.Class<? extends Payload>[] payload() default {};
The default value must be an empty array.
Each attachable payload extendsPayload
./**
* Payload type that can be attached to a given
* constraint declaration.
* <p/>
* Payloads are typically used to carry on metadata information
* consumed by a validation client.
* </p>
* Use of payloads is not considered portable.
*/
最佳答案
也许这个 link会帮助你。
import javax.validation.*;
import javax.validation.constraints.NotNull;
import java.util.Set;
public class ConstraintPayloadExample2 {
private static final Validator validator;
static {
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
validator = factory.getValidator();
factory.close();
}
public interface AppErrorHandler<T> extends Payload {
void onError (ConstraintViolation<T> violation);
}
public static class ErrorEmailSender<T> implements AppErrorHandler<T> {
@Override
public void onError (ConstraintViolation<T> violation) {
System.out.println("Sending email to support team: " +
violation.getPropertyPath() + " " +
violation.getMessage());
}
}
public static class TestBean {
@NotNull(payload = {ErrorEmailSender.class})
private String str;
public String getStr () {
return str;
}
public void setStr (String str) {
this.str = str;
}
}
public static void main (String[] args) {
TestBean bean = new TestBean();
Set<ConstraintViolation<TestBean>> constraintViolations =
validator.validate(bean);
if (constraintViolations.size() > 0) {
constraintViolations.stream().forEach(
ConstraintPayloadExample2::processError);
} else {
//proceed using user object
System.out.println(bean);
}
}
private static void processError (ConstraintViolation<TestBean> violation) {
Set<Class<? extends Payload>> payload =
violation.getConstraintDescriptor().getPayload();
payload.forEach(p -> {
if (AppErrorHandler.class.isAssignableFrom(p)) {
try {
AppErrorHandler errorHandler = (AppErrorHandler) p.newInstance();
errorHandler.onError(violation);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
关于jakarta-ee - Bean Validation API 中 javax.validation.Payload 的用例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018356/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!