We have something typical restulApi as below:

Mono<Void> apiMethod(@Valid @RequestBody Mono<Request> requestMono)
{
//....
}

public Class Request{
@NotNull
private String id;
...
}

But I found the validation annotation is no longer working. The invalid requests with null id will still pass to the inner methods.

Not sure if it's a bug or do I miss anything particularly for spring boot 2.

Thanks

Comment From: bclozel

Thanks for getting in touch, but it feels like this is a question that would be better suited to StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Comment From: duke-cliff

Okay, I found the annotation should add within Mono/Flux, e.g. Mono<@Valid Request> ...

It works, but I do believe with more and more people using webflux, this could be an enhancement to improve.

Comment From: NaccOll

@Valid doesn't work due to lack of dependencies, the following actions can make @Valid take effect:

<dependency>
       <groupId>org.glassfish</groupId>
       <artifactId>javax.el</artifactId>
       <version>3.0.1-b09</version>
</dependency>

You can also add the following dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

But, spring-boot-starter-validation include spring-web. Can you add javax.el to spring-boot-starter-webflux? @bclozel

Comment From: meetshahblogs

Was this ever fixed? I am using Spring boot version 2.0.6 and @valid annotation not working.

Comment From: snicoll

@meetshahblogs please take the time to read the issue you're commenting on. This was raised as a question that we redirected to StackOverflow. I suggest you do the same. If you believe you've found a bug in Spring Boot (nothing here indicates so), create a separate issue with a sample we can run ourselves (zip or repo link).

Comment From: fetech-zhanggang

So what is the solution? I meet the same issue and can not find any good solutions in the StackOverflow or google @snicoll @bclozel

Comment From: NaccOll

@fetech-zhanggang https://github.com/spring-projects/spring-boot/issues/14080#issuecomment-450779899

Comment From: abhi2495

@bclozel

Even though it is mentioned in the doc, it looks like bean validations are not working . I request that this issue being looked upon and reopened

Comment From: wilkinsona

@abhi2495 There's no point in re-opening the issue without some evidence that it doesn't work. If validation isn't working for you, I suspect that's because you do not have spring-boot-starter-validation on your classpath. If you do have the necessary dependencies in place and it still isn't working, then we can spend some time investigating but, before we do so, you'll have to spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Comment From: abhi2495

@wilkinsona @bclozel Here is a sample: https://github.com/abhi2495/demo-svc I dont know what I am missing or doing wrong here. I would greatly appreciate if you please look into this.

Comment From: wilkinsona

Thanks for the sample, @abhi2495.

The first problem is that you do not have the necessary validation dependencies. As I suggested above, the recommended way to add them is by using spring-boot-starter-validation:

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

Note that I've had to specify the version here as you aren't making use of Spring Boot's dependency management. We recommend that you make use of it by using spring-boot-starter-parent as your Maven project's parent. Doing so will allow you to remove the <version> from this and several other dependencies, thereby making it easier to keep all the versions in sync.

The second problem is that you aren't doing anything with the EmployeeContract mono. When using WebFlux, processing is only performed when it's required. Unless you attempt to access the EmployeeContract no validation will be performed. You will get a 400 response if you make a POST request without a name and age if you change your controller method to something like the following:

@PostMapping("/api/employee")
public Mono<ResponseEntity<String>> save(@Valid @RequestBody Mono<EmployeeContract> employeeMono) {
    return employeeMono.map((employee) -> ResponseEntity.ok(employee.getName()));
}

Comment From: abhi2495

Thanks @wilkinsona for pointing it out. My bad. I forgot about the whole mono thing, that they are not processed until required.

Comment From: anubhav102889

@wilkinsona it means that request body validation with mono type will happen only and only when the request body is processed as suggested in your comment through example. Unlike in the case of request body without mono the validation is performed even before processing.