This works:

@Validated
@ConfigurationProperties("com.google.x")
class MyProperties {
    @URL
    lateinit var url: String
    @NotBlank
    lateinit var userName: String
    @NotBlank
    lateinit var password: String
}

This doesn't:

@Validated
@ConstructorBinding
@ConfigurationProperties("com.google.x")
data class MyProperties(
    @URL
    val url: String,
    @NotBlank
    val userName: String,
    @NotBlank
    val password: String
)

I'm using @EnableConfigurationProperties(MyProperties::class) in the class who reads these properties.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

Comment From: wilkinsona

Thanks for the report. Validation isn't working due to the way in which Kotlin handles annotations in a data class and how Hibernate Validator looks for those annotations.

Hibernate Validator looks for constraint annotations on the fields or getter methods for each property but Kotlin is placing them on the constructor parameters. You need to configure annotation use-site targets to tell Kotlin to place the annotations on the fields or the getters. Instructing Kotlin to place them on the fields looks like this:

@field:URL
val url: String,
@field:NotBlank
val userName: String,
@field:NotBlank
val password: String

Comment From: GabrielBB

Thanks for the quick response. Finally working :D