Jakarta @Nonnull does not cause validation error in Spring

181 Views Asked by At

I'm studying validation in Spring and jakarta. I created rest simple controller:

@RestController
class EmailController {

    @PostMapping(path = ["/email"])
    fun post(
        @RequestBody @Validated email: SendEmailRequest
    ) : SendEmailRequest {
        return email
    }
}

and request class with nested classes:

class Request {
    @Nonnull
    @Valid
    val email: Letter? = null
}

class Letter {
    @Nonnull
    @Valid
    val receiver: EmailContact? = null

    @NotBlank
    val header: String = ""
    val plainText: String = ""
    val htmlContent: String = ""
}

Now I'm trying to send requests with Postman. The body of request is

{
    "email": {
        "header": ""
    }
}

The expected behavior is:

{
    "email.receiver": [
        "must not be null"
    ],
    "email.header": [
        "must not be blank"
    ]
}

The actual behavior (actual response) is:

{
    "email.header": [
        "must not be blank"
    ]
}

Why @Nonnull is not working?

0

There are 0 best solutions below