Search code examples
springspring-mvchibernate-validatorspring-webflux

Spring 5 Webflux functional endpoints - How to perform input validation?


According to the current doc (5.0.0.RELEASE) Spring Webflux supports validation when working with annotated controllers:

By default if Bean Validation is present on the classpath — e.g. Hibernate Validator, the LocalValidatorFactoryBean is registered as a global Validator for use with @Valid and Validated on @Controller method arguments.

However nothing is said about how to automate it with functional endpoints. In fact, the only example of input processing in the documentation doesn't validate anything:

public Mono<ServerResponse> createPerson(ServerRequest request) { 
    Mono<Person> person = request.bodyToMono(Person.class);
    return ServerResponse.ok().build(repository.savePerson(person));
}

Are we supposed to do this manually or there is some automatic way to do it?


Solution

  • In Spring version 5.0, there is no automatic way to do validation in functional endpoints, and as such validation must be done manually.

    Though there are currently no concrete plans to do so, we might add some sort of validation in the future. But even then it will be an explicit method call, and not an automatic mechanism. Overall, the functional endpoint model is designed to be a lot more explicit than the annotation-based model.