找不到webflux的返回

w3nuxt5m  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(188)

我很难在找不到身份证时返回404。添加 .defaultIfEmpty(Mono.just(ResponseEntity.notFound().build())) 对这些语句抛出编译错误,抱怨结果是 ResponseEntity<Object> 而不是 ResponseEntity<Void> .
这是我的第一个webflux项目,我很难找到缓解措施。
售后服务。java:86:错误:不兼容的类型:mono无法转换为mono.map(repository::save)。然后返回(nocontent().build()).defaultifempty(notfound().build());

// TODO: Not found, DataIntegrityViolation
  public Mono<ResponseEntity<Void>> updatePost(
      UUID postId, Mono<UpdatePostRequest> updatePostRequest) {
    return REPOSITORY
        .findById(postId)
        .zipWith(updatePostRequest)
        .map(
            zip ->
                zip.getT1().toBuilder()
                    .title(
                        zip.getT2().getUpdateMask().contains(PostUpdateMask.TITLE)
                            ? zip.getT2().getTitle()
                            : zip.getT1().getTitle())
                    .summary(
                        zip.getT2().getUpdateMask().contains(PostUpdateMask.SUMMARY)
                            ? zip.getT2().getSummary()
                            : zip.getT1().getSummary())
                    .markdown(
                        zip.getT2().getUpdateMask().contains(PostUpdateMask.MARKDOWN)
                            ? zip.getT2().getMarkdown()
                            : zip.getT1().getMarkdown())
                    .imageUrl(
                        zip.getT2().getUpdateMask().contains(PostUpdateMask.IMAGE_URL)
                            ? zip.getT2().getImageUrl()
                            : zip.getT1().getImageUrl())
                    .build())
        .flatMap(REPOSITORY::save)
        .thenReturn(noContent().build());
  }

  // TODO: Not found
  public Mono<ResponseEntity<Void>> deletePost(UUID postId) {
    return REPOSITORY
        .existsById(postId)
        .then(REPOSITORY.deleteById(postId))
        .thenReturn(noContent().build());
  }

编辑:deletepost通过以下方法修复

public Mono<ResponseEntity<Void>> deletePost(UUID postId) {
       Mono<ResponseEntity<Void>> response = REPOSITORY
            .findById(postId).map(REPOSITORY::delete).thenReturn(noContent().build());
    return response.defaultIfEmpty(notFound().build());
  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题