简体   繁体   English

如何在 Spring web flux 中编写 Update API?

[英]How to write Update API in Spring web flux?

I am pretty new to Spring web flux reactive programming.我对 Spring Web Flux 反应式编程还很陌生。 So this is little confusing.所以这有点令人困惑。

I am writing update API with spring web flux.我正在用 spring web flux 编写更新 API。 Passed request body tp the update method is a Mono of request.传递的请求体 tp update 方法是请求的 Mono。 So My problem is How can I get the request body and perform with?所以我的问题是如何获取请求正文并执行? . . Please check the commented line in following code.请检查以下代码中的注释行。

public Mono<User> updateUser(String userId, Mono<UserRequest> user){

    return userRepository.findById(userId)
        .switchIfEmpty(Mono.error(new NotFoundException("User not found")))
        .flatMap(foundUser -> {
            // How to get passed user object from Mono<UserRequest>
            modelMapper.map(source, foundUser);
            return Mono.just(userRepository.save(foundUser))
        })
}

Thank you.谢谢你。

You can add .zipWith(user, (foundUser, user) ->... (or simples .zipWith(user) that returns Tuple ).您可以添加.zipWith(user, (foundUser, user) ->... (或返回Tuple的简单.zipWith(user) )。

Samples:样本:

    @Test
    void x1() {
        Mono<String> m1 = Mono.just("S1");
        Mono<String> m2 = Mono.just("S2");

        m1
            .switchIfEmpty(Mono.error(new NotFoundException("User not found")))
            .zipWith(m2, (s1, s2) -> s1.concat(s2)) // can be simplified to 'String::concat'
            .as(StepVerifier::create)
            .assertNext(s -> assertThat(s)
                .isEqualTo("S1S2")
            )
            .verifyComplete();
    }

    @Test
    void x2() {
        Mono<String> m1 = Mono.just("S1");
        Mono<String> m2 = Mono.error(new RuntimeException("some-error"));

        m1
            .switchIfEmpty(Mono.error(new NotFoundException("User not found")))
            .zipWith(m2, String::concat)
            .as(StepVerifier::create)
            .expectErrorSatisfies(throwable -> assertThat(throwable)
                .isInstanceOf(RuntimeException.class)
                .hasMessage("some-error")
            )
            .verify();
    }

    @Test
    void x3() {
        Mono<String> m1 = Mono.error(new RuntimeException("some-error"));
        Mono<String> m2 = Mono.just("S2");

        m1
            .switchIfEmpty(Mono.error(new NotFoundException("User not found")))
            .zipWith(m2, String::concat)
            .as(StepVerifier::create)
            .expectErrorSatisfies(throwable -> assertThat(throwable)
                .isInstanceOf(RuntimeException.class)
                .hasMessage("some-error")
            )
            .verify();
    }

    @Test
    void x4() {
        Mono<String> m1 = Mono.empty();
        Mono<String> m2 = Mono.just("S2");

        m1
            .switchIfEmpty(Mono.error(new NotFoundException("User not found")))
            .zipWith(m2, String::concat)
            .as(StepVerifier::create)
            .expectErrorSatisfies(throwable -> assertThat(throwable)
                .isInstanceOf(NotFoundException.class)
                .hasMessage("User not found")
            )
            .verify();
    }

Btw, there is a super-handy documentation from reactor: https://projectreactor.io/docs/core/release/reference/#which-operator顺便说一句,反应堆有一个非常方便的文档: https ://projectreactor.io/docs/core/release/reference/#which-operator

I am also wondering why you pass Mono<UserRequest> user - where does it come from and why you cannot use plain UserRequest user ?我也想知道为什么你传递Mono<UserRequest> user - 它来自哪里以及为什么你不能使用普通UserRequest user

Also, your repository is suspicious - why findById returns a reactive object, while save does not?此外,您的存储库很可疑 - 为什么findById返回一个反应对象,而save却没有? It looks like a blocking call, hence you loose benefits of webflux/reactor.它看起来像一个阻塞调用,因此你失去了 webflux/reactor 的好处。

// How to get passed user object from Mono // 如何从 Mono 获取传递的用户对象

Start with that Mono (the repository calls are non-reactive operations anyway):从那个 Mono 开始(存储库调用无论如何都是非反应性操作):

public Mono<User> updateUser(String userId, Mono<UserRequest> user){
    return user.map(request -> ...);
}

Read: Once the UserRequest object is available, do...阅读:一旦 UserRequest 对象可用,请执行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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