在春季webFlux中获取WebExchangeBindException代替HttpMessageNotReadableException

时间:2018-12-24 12:51:21

标签: spring spring-boot spring-webflux reactive

我已经重构了代码,以使用spring webflux代替spring MVC,但是现在每当bean验证失败时,contoller建议中就会用WebExchangeBindException代替HttpMessageNotReadableException

public Flux<UserContactsModel> getUserContacts(@RequestBody  @Valid LoginModel loginDetail) {
    return contactInfoService
        .getUserContacts(loginDetailApiMapper.loginModelMonoToLoginBoMono(Mono.just(loginDetail)))
        .flatMapIterable(
    userContactsBO -> contactInfoMapper.userContactBoToModelList(userContactsBO));
}

控制器建议

@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> genericException(Exception ex) {
    return new ResponseEntity<>(new Message(responseBody), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(value = HttpMessageNotReadableException.class)
protected ResponseEntity<Object> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex) {

    return new ResponseEntity<>(new Message("Bad Request", reason), HttpStatus.BAD_REQUEST);
}

0 个答案:

没有答案