I am using Spring Boot validation to validate some fields.
I've created the ValidationMessages.properties
file under resources and overwritten the value for the default validation of Size as follows:
javax.validation.constraints.Size.message=Please enter a value. Maximum length is {max}.
Which works like a charm, at runtime the {max}
token is replaced with the value from the annotation @Size(max = 100)
.
Now I want to define a custom proprietary entry, something like:
my.custom.message=Hey, my custom value is {customValue}
The question is - how can I replace the {customValue}
token at runtime starting from something like?
private static final String CUSTOM_STRING = "{my.custom.message}";
In the context of validation, you must use something like:
@Documented
@Constraint(validatedBy = MyValidator.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
int customValue() default "THIS IS CUSTOM"; // this will be the value that will be injected in the custom message,
// and can be changed when using the annotation
String message() default "{my.custom.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Also, if you use private static final String CUSTOM_STRING = "{my.custom.message}";
in the context of validation, the replacement will be done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.