0

I have a Spring Boot project (currently using 2.2.6), with web starter.

In some classes I use validation annotations like

@Size(max = 1, min = 2)

or

@Pattern(regexp = "[0-9]+")

on fields, from javax.validation.constraints

It works as expected, but the values for the annotations are hard coded. I would like to set them dynamically, for example reading from a properties file, because in some situations the values might be different.

Is that possible, how to do it?

neblaz
  • 683
  • 10
  • 32
  • Did you check this answer https://stackoverflow.com/questions/10636201/java-annotations-values-provided-in-dynamic-manner – Mohit Singh May 13 '20 at 07:07
  • Didn't find that. The latest answer is from 2017, explains two methods, still that needs to be understood, don't know exactly how to do first approach. – neblaz May 13 '20 at 07:13

3 Answers3

0

These annotations are implemented under the hood by Hibernate Validator project.

The default messages coming from annotation values appear due to the Default Interpolator (the concept of Hibernate validator).

So the solution, I believe should consist of two steps:

  1. Create a custom Message Interpolator in terms of spring Validator project. Take a loot here for details, they have an example of message interpolator that reads from some file system resource. At this point this has nothing to do with spring / spring boot yet.

  2. Integrate the message interpolator that you've created during Step 1 with spring boot application.

For that your code should include something like this:

Validation.byDefaultProvider().configure().messageInterpolator(
  new MyCustomMessageInterpolator(
    Validation.byDefaultProvider().configure().getDefaultMessageInterpolator())
);

I've found a tutorial about such an integration here

This tutorial seems to not telling where exactly you should put this code, so I think you should try some listener that will listen to "ApplicationStarted" event or something

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

I think in Spring boot values in annotations have to be hardcoded and can not be set as placeholders for property values. I am not completely sure about it and maybe wrong but I think this is the case. A while ago, I dealt with a similar issue for scheduled job runner, and I ended up running my own custom code that allows parsing time intervals and other custom needed properties from properties files. I included that feature into my own Open source library so it could be re-used. Here you can read about this particular feature (JavaDoc). In general, if you are interested you can read about the library here and get it from Git (Including sources and JavaDoc) or Maven Central. Just JavaDoc could be seen here

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

Perhaps this is also possible solution, but requires to define custom annotations, so the build-in annotations from javax.validatation.constraints can't be used:

Spring Bean Validation with configurable constraint values

neblaz
  • 683
  • 10
  • 32