Spring does not create ConstraintValidator

Spring does not create an instance of class implementing ConstraintValidator. Even when I annotate RolesValidator with @Configuartion Spring create its instance, but validation doesn’t work.

Here is my code:

@Constraint(validatedBy = [RolesValidator::class])
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
annotation class ValidateRoles(
    val message: String = "{com.app.authservice.validators.ValidateRoles.message}",
    val groups: Array<KClass<*>> = arrayOf(),
    val payload: Array<KClass<out Payload>> = arrayOf()


class RolesValidator : ConstraintValidator<ValidateRoles, Collection<String>> {
    lateinit var allowedValues: List<String>

    override fun initialize(constraintAnnotation: ValidateRoles?) {
        allowedValues = RoleType.values().map { it.name }

    override fun isValid(value: Collection<String>?, context: ConstraintValidatorContext?): Boolean {
        return allowedValues.containsAll(value!!)


class AccountUpdateRolesDTO {
    var roles: Set<String> = emptySet()


fun updateRoles(
    @RequestBody @Valid body: AccountUpdateRolesDTO,
    @PathVariable accountId: Long,
    jwt: JWTData
): ResponseEntity<Void> {
    return ResponseEntity(HttpStatus.OK)

@edit https://jira.spring.io/browse/SPR-16701


If you leverage annotation use-site targets, Kotlin tests pass as well:

class KotlinBodyDTO {
    var roles: Set<String> = emptySet()

Leave a Reply

Your email address will not be published. Required fields are marked *