簡體   English   中英

bean驗證不適用於spring webflux

[英]Bean validation is not working for spring webflux

我已經重構了我的代碼以使用 spring webflux 但現在@Valid停止工作。 它沒有驗證請求正文。

@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));
}

我得到 200 OK 代替我從控制器建議返回的 Bad request。

編輯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;
    }

更新1:更改這樣的代碼並在類級別添加@Validated

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

我收到 javax.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'T' of type reactor.core.publisher.Mono。

@Valid批注驗證對象。 因此,您嘗試驗證Mono時,需要更改為LoginModel對象,例如:

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

什么都沒有為我工作。 因此,我使用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;
  }

對我來說,@ @Valid開箱即用,缺少的部分是在類路徑中添加spring-boot-starter-validation

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

我有與這個問題完全相同的控制器代碼加上一個ExceptionHandler來處理 bean 驗證錯誤:

@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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM