Close

JavaBean Validation - Using Expression Language Examples

JavaBean Validation JAVA EE 

This example overrides the default message key and provides application level resource bundle files.

package com.logicbig.example;

import javax.validation.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Locale;

public class ProvidingDifferentKeyExample {

private static class MyBean {
private String myString;

@NotNull(message = "{myBean.myString.null.msg}")
@Pattern(regexp = "\\D+", message = "{myBean.myString.pattern.msg}")
public String getMyString() {
return myString;
}

public void setMyString(String myString) {
this.myString = myString;
}
}

public static void main(String[] args) {
MyBean myBean = new MyBean();
//myBean.setMyString("jackie10");

Locale.setDefault(Locale.US);//Locale.FRANCE
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
validator.validate(myBean).stream().forEach(ProvidingDifferentKeyExample::printError);
factory.close();
}

private static void printError(
ConstraintViolation<MyBean> violation) {
System.out.println(violation.getMessage());
}
}

Output

myString cannot be null

src/main/resources/ValidationMessages_en.properties:

myBean.myString.null.msg = myString cannot be null
myBean.myString.pattern.msg = myString shouldn't contain numbers. regex={regexp}, value found=${validatedValue}
javax.validation.constraints.Future.message = Date must be in future. found: ${validatedValue}
Original Post




Using inline messages and expression within them.

package com.logicbig.example;

import org.hibernate.validator.internal.engine.ConfigurationImpl;
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel;
import javax.validation.*;
import javax.validation.constraints.Size;

public class ValidatedValueExample {

private static class TestBean {

@Size(min = 5, message = "The name '${validatedValue}' must be at least {min}" +
" characters long. Length found: '${validatedValue.length()}'")
private String name;

public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}
}

public static void main (String[] args) {
TestBean testBean = new TestBean();
testBean.setName("Mike");

Configuration<?> config = Validation.byDefaultProvider().configure();

//6.2.0.Final
((ConfigurationImpl)config).constraintExpressionLanguageFeatureLevel(
ExpressionLanguageFeatureLevel.BEAN_METHODS);

ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
validator.validate(testBean).stream().forEach(ValidatedValueExample::printError);
factory.close();
}

private static void printError (
ConstraintViolation<TestBean> violation) {

System.out.println(violation.getMessage());
}
}

Output

The name 'Mike' must be at least 5 characters long. Length found: '4'
Original Post




In this example we are overriding the default key for the constraint 'Future' by providing a new value for the key in our resource file.

package com.logicbig.example;

import javax.validation.*;
import javax.validation.constraints.Future;
import java.util.Date;
import java.util.Locale;

public class OverrideDefaultKeyExample {

private static class MyBean {
@Future
private Date date;

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}

public static void main(String[] args) {
MyBean myBean = new MyBean();
myBean.setDate(new Date(System.currentTimeMillis() - 100000));

Locale.setDefault(Locale.US);//Locale.FRANCE
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();

validator.validate(myBean).stream()
.forEach(OverrideDefaultKeyExample::printError);

factory.close();
}

private static void printError(
ConstraintViolation<MyBean> violation) {
System.out.println(violation.getMessage());
}
}

Output

Date must be in future. found: Wed Jul 28 03:00:29 CDT 2021

src/main/resources/ValidationMessages_en.properties:

myBean.myString.null.msg = myString cannot be null
myBean.myString.pattern.msg = myString shouldn't contain numbers. regex={regexp}, value found=${validatedValue}
javax.validation.constraints.Future.message = Date must be in future. found: ${validatedValue}
Original Post




See Also