gpt4 book ai didi

jakarta-ee - Bean Validation API 中 javax.validation.Payload 的用例是什么?

转载 作者:行者123 更新时间:2023-12-04 11:45:38 30 4
gpt4 key购买 nike

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 extends Payload.

/**
* 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.
*/


我已经阅读了它的示例,但我不明白,例如,如何在 JSF 中使用此元数据?你能解释一下现实世界中有效载荷的其他用例吗? Payload携带的元数据是什么?

最佳答案

也许这个 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com