Spring Webflux Bean Validation Not Working

I am trying to use bean validation in Webflux. This is what I have so far:

fun create(@RequestBody @Valid contact: Mono<Contact>) : Mono<Contact> {
    return contact.flatMap { }
            .doOnError{ Error("test") }

The The validation doesn't work... I would expect that the Error("test") would be shown...

Does someone has a working example(Java or Kotlin)?


Here is a repository so it can be reproducted:


curl --request POST \
  --url http://localhost:8080/tickets \
  --header 'content-type: application/json' \
  --data '{
    "email": "",
    "name": "",
    "message": ""

Renamed contact to ticket, but everything is still the same.



The annotations you have placed in the example project are actually annotations on the constructor parameters of the Ticket class. For Spring validation, you need to annotate the fields instead. You can do this in Kotlin by using annotation use-site targets.

In this specific case, your Ticket class should look like this:

data class Ticket(
        @field:JsonSerialize(using = ToStringSerializer::class)
        val id: ObjectId = ObjectId.get(),

        val email: String,

        @field:Size(min = 2, max = 200)
        val name: String,

        @field:Size(min = 10, max = 2000)
        val message: String

This, along with the following controller function will work and return errors as expected:

fun create(@RequestBody @Valid contact: Mono<Ticket>) : Mono<Ticket> {
    return contact.flatMap { }
            .doOnError{ Error("test") }