简体   繁体   中英

Bean validation is not working for spring webflux

I have refactored my code to use spring webflux but now @Valid stopped working. It is not validating the request body.

@PostMapping(value = "/getContactInfo",produces = "application/json",consumes = "application/json")
public Flux<UserContactsModel> getUserContacts(@Valid @RequestBody Mono<LoginModel> loginDetail) {

    loginDetail.log();
    return contactInfoService
        .getUserContacts(loginDetailApiMapper.loginModelMonoToLoginBoMono(loginDetail))
        .flatMapIterable(
            userContactsBO -> contactInfoMapper.userContactBoToModelList(userContactsBO));
}

I am getting 200 OK in place of Bad request which I am returning from the controller advice.

Edit 1:

   import javax.validation.constraints.NotNull;
   import javax.validation.constraints.Pattern;

    public class LoginModel implements Serializable {

      private String clientId;

      @Pattern(regexp = "^[a-zA-Z0-9]*$", message = "Login ID is invalid")
      @NotNull
      private String loginId;
    }

update 1: After changing the code like this and adding @Validated on class level

@RestController
@Validated
public class ContactInfoController implements ContactInfoApi {
public Flux<UserContactsModel> getUserContacts(@RequestBody  Mono<@Valid  LoginModel> loginDetail) {

I am getting javax.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'T' of type reactor.core.publisher.Mono.

@Valid annotation validates an object. So you are trying to validate a Mono, you need to change to LoginModel object, for example:

  ..getUserContacts(@RequestBody Mono<@Valid LoginModel> loginDetail) {
      ...
  }

Nothing worked for me. So I validated it manually by using javax.validator.

@Autowired private Validator validator;

 public Flux<UserContactsModel> getUserContacts(@RequestBody Mono<@Valid LoginModel> loginDetail) {

    return loginDetail
        .filter(this::validate)
        .map(....);
}

 private boolean validate(LoginModel loginModel) {

    Set<ConstraintViolation<LoginModel>> constraintViolations = validator.validate(loginModel);

    if (CollectionUtils.isNotEmpty(constraintViolations)) {
      StringJoiner stringJoiner = new StringJoiner(" ");
      constraintViolations.forEach(
          loginModelConstraintViolation ->
              stringJoiner
                  .add(loginModelConstraintViolation.getPropertyPath().toString())
                  .add(":")
                  .add(loginModelConstraintViolation.getMessage()));
      throw new RuntimeException(stringJoiner.toString());
    }

    return true;
  }

For me the @Valid worked out of the box, the missing piece was adding spring-boot-starter-validation in the classpath:

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

I have the exact same controller code as in this question plus a ExceptionHandler to handle the bean validation errors:

@ResponseBody
@ExceptionHandler(WebExchangeBindException.class)
Mono<ResponseEntity<List<ErrorDTO>>> invalidRequestErrorHandler(@NotNull final WebExchangeBindException e) {
    log.error("Invalid request exception occurred", e);
    var errors = e.getBindingResult()
            .getAllErrors()
            .stream()
            .filter(Objects::nonNull)
            .map(this::getValidationErrorMessage)
            .toList();
    return Mono.just(ResponseEntity.status(BAD_REQUEST)
            .contentType(APPLICATION_JSON)
            .body(errors));
}

@NotNull
private ErrorDTO getValidationErrorMessage(@NotNull final ObjectError error) {
    final var errorMessage = new StringBuilder();
    if (error instanceof FieldError fe) {
        errorMessage.append(fe.getField()).append(" - ");
    }
    errorMessage.append(error.getDefaultMessage());
    return new ErrorDTO()
            .errorCode(GENERIC_ERROR).message(errorMessage.toString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM