This is quite a niche question but hopefully someone knows the answer:
I have a SpringFramework RestController with a GetMapping, that has some parameter validation using javax.validation annotations. I would like to use a property that's defined in application.yml, as the max value for a url parameter, but I get the compiler error that the variable should be a constant. See code:
@RestController
@Validated
public class AssetController {
private final int maxWaitMillis;
@Autowired
public AssetController(@Value("${maxWaitMillis}") int maxWaitMillis) {
this.maxWaitMillis = maxWaitMillis;
}
@GetMapping(value = "/asset", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public ResponseEntity<byte[]> getAsset(@RequestParam
@NotBlank(message = "id should not be empty") String id,
@RequestParam(defaultValue = "100", name = "timeout-ms")
@Min(value = 0, message = "minimum timeout is 0 ms")
@Max(value = maxWaitMillis, message = "max timeout is " +
maxWaitMillis) int timeoutMs) {
...
}
application.yml:
maxWaitMillis: 5000
At @Max(value = maxWaitMillis it says maxWaitMillis should be constant, because I guess the check within the annotation is done before the constructor can get the value from application.yml.
If I check the value of maxWaitMillis
within the constructor or method body it does have a value of 5000.
Does anyone know if there's a way to use the value from application.yml in the validation annotations?
Thanks!