Close

JavaBean Validation - javax.validation.Payload Examples

JavaBean Validation JAVA EE 

This example shows the use of constraint element 'payload'. Here we are using reflection to invoke some code associated with payload.

package com.logicbig.example;

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();
}
}
});
}
}

Output

Sending email to support team: str may not be null
Original Post




This example shows how can we attach some metadata information with a given constraint declaration by using it's element 'payload'. In this example we are going to make decision on the level of error severity to take action.

package com.logicbig.example;

import javax.validation.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Set;

public class ConstraintPayloadExample {
private static final Validator validator;

static {
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
validator = factory.getValidator();
factory.close();
}

public static class Severity {
public static class Info implements Payload {
}

public static class Error implements Payload {
}
}

public static class TestBean {
@NotNull(payload = {Severity.Error.class})
@Size(min = 1, payload = {Severity.Info.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();
//uncommenting next line will give us info severity
// or setting an non-empty string won't give any validation error
//bean.setStr("");

Set<ConstraintViolation<TestBean>> constraintViolations =
validator.validate(bean);

boolean severeError = false;

if (constraintViolations.size() > 0) {
for (ConstraintViolation<TestBean> violation : constraintViolations) {
Set<Class<? extends Payload>> payloads =
violation.getConstraintDescriptor().getPayload();
for (Class<? extends Payload> payload : payloads) {
if (payload == Severity.Error.class) {
severeError = true;
System.out.println("Error: " + violation.getPropertyPath() + " " +
violation.getMessage());
} else if (payload == Severity.Info.class) {
System.out.println("Info: " + violation.getPropertyPath() + " " +
violation.getMessage());
}
}
}
}

if (!severeError) {
//continue working with bean
System.out.println("working with : " + bean);
}
}

}

Output

Error: str may not be null
Original Post




See Also