Spring Reactive WebFlux--如何定制BadRequest错误消息 [英] Spring Reactive WebFlux - how to customize the BadRequest error message

查看:14
本文介绍了Spring Reactive WebFlux--如何定制BadRequest错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的请求处理程序中,如果传入的accountId不能转换为有效的ObjectId,我希望捕获错误并发回有意义的消息;然而,这样做会导致返回类型不兼容,并且我不知道如何实现这个相当简单的用例。

我的代码:

  @GetMapping("/{accountId}")
  public Mono<ResponseEntity<Account>> get(@PathVariable String accountId) {
      log.debug(GETTING_DATA_FOR_ACCOUNT, accountId);

      try {
        ObjectId id = new ObjectId(accountId);
        return repository.findById(id)
            .map(ResponseEntity::ok)
            .switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
      } catch (IllegalArgumentException ex) {
        log.error(MALFORMED_OBJECT_ID, accountId);
        // TODO(marco): find a way to return the custom error message. This seems to be currently
        //  impossible with the Reactive API, as using body(message) changes the return type to
        //  be incompatible (and Mono<ResponseEntity<?>> does not seem to cut it).
        return Mono.just(ResponseEntity.badRequest().build());
      }
  }

body(T body)方法更改返回的Mono的类型,使其为String)Mono<ResponseEntity<String>>;但是,将该方法的返回类型更改为Mono<ResponseEntity<?>>也不起作用:

        ...
        return Mono.just(ResponseEntity.badRequest().body(
            MALFORMED_OBJECT_ID.replace("{}", accountId)));

因为它在另一个return语句中给出了不兼容的类型错误:

error: incompatible types: Mono<ResponseEntity<Account>> cannot be converted to Mono<ResponseEntity<?>>
            .switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));

显然,将方法的返回类型更改为Mono<?>是可行的,但随后的响应是ResponseEntity的序列化JSON,这不是我想要的。

我也尝试过使用onErrorXxxx()方法,但它们在这里也不起作用,因为转换错误甚至在计算通量之前就发生了,而且我只得到了一个带有空消息的";vanilla";400错误。

我唯一能想到的解决方法就是向Account对象添加一个message字段并返回该字段,但这确实是一个可怕的黑客攻击。

推荐答案

@Thomas-andolf的回答帮助我找到了实际的解决方案。

对于将来遇到这个问题的任何人来说,我实际上是如何解决这个难题的(当然,您仍然需要try/catch来拦截ObjectId构造函数抛出的错误):

  @GetMapping("/{accountId}")
  public Mono<ResponseEntity<Account>> get(@PathVariable String accountId) {
    return Mono.just(accountId)
        .map(acctId -> {
          try {
            return new ObjectId(accountId);
          } catch (IllegalArgumentException ex) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
                MALFORMED_OBJECT_ID));
          }
        })
        .flatMap(repository::findById)
        .map(ResponseEntity::ok)
        .switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
  }

要真正看到返回的Body中的message,需要在application.properties中添加server.error.include-message=always(参见here)。

使用onError()在这里不起作用(我确实在它的所有变体中尝试过),因为它需要Mono<ResponseEntity<Account>>,并且无法从错误状态生成一个(在添加消息正文时)。

这篇关于Spring Reactive WebFlux--如何定制BadRequest错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆