Spring-Webflux:在没有 block() 的情况下从 Mono 中提取对象 [英] Spring-Webflux: Extracting Object from Mono without block()

查看:35
本文介绍了Spring-Webflux:在没有 block() 的情况下从 Mono 中提取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的 Spring Webflux.我正在编写一个简单的 api,它调用另一个 api 并返回响应.我的问题是我的 api 接受的请求类型与外部 api 不同.我必须将传入的请求转换为发送到外部 api.我正在使用 Mono 来接收对我的 api 的请求,但是在没有转换到另一个对象时遇到了麻烦块().

I am new Spring Webflux. I am writing a simple api which call another api and returns response. The problem I have is my api takes diffrent type of request than the external api.I have to convert the incoming request to send to external api.I am using Mono to receive request for my api, but having trouble to convert to another object without block().

输入

路由器

@Configuration
@EnableWebFlux
public class RouterConfig implements WebFluxConfigurer{

    @Bean
    public RouterFunction<ServerResponse> routes(UserHandler handler){
        
        return RouterFunctions
                .route(POST("/rest/create"),
                        handler::createUser);
    }
    
} 

处理程序

@Component
public class UserHandler {

    private UserService service;

    public UserHandler(UserService service) {
    this.service = service;
    }

      
    public Mono<ServerResponse> saveUser(ServerRequest request) 
    {
    
        Mono<User> user = request.bodyToMono(User.class)
    
        /* currently I am using block to get User object */
    
         User user1 = user.block()
    
        /* convert user to person */
       
           Person p =getPersonFromUser(user);
    
       
    
        
    }

Pojos

 class User
    {
      private String name;
      private String id;
      private String email;
       private String phone;    
    
    
    }



 class Person
    {
      private String email;
      /* Combination of id and name */
      private String accountNumber;
      private String phone;
    
    }

有没有一种方法可以在不阻塞的情况下将 Mono 转换为 Person 对象?

Is there a way I can convert the Mono to Person object without blocking?

推荐答案

如果您愿意在 Mono-Lambda 中处理 Person p,那么您可以尝试

If you're willing to have Person p processed in a Mono-Lambda, then you can try

public Mono<ServerResponse> saveUser(ServerRequest request) 
{
    // Mono<User> user = request.bodyToMono(User.class)
    request.bodyToMono(User.class)

    // Convert a Mono<User> to a Mono<Person>    
    .map((User user1) -> getPersonFromUser(user))

    .map((Person p) -> {
        // Code that either returns ServerResponse object or an intermediary object
    })
    // Additional code as needed

因为 request.bodyToMono(User.class) 返回用户的 Mono,您可以调用该 Mono 的 map 方法.map 方法接受 函数,它接受一种类型的参数并返回不同类型的对象.

Because request.bodyToMono(User.class) returns a Mono of a User, you can call that Mono's map method. the map method takes in a lambda of type Function which takes in a parameter of one type and returns an object of a different type.

第一次调用地图:

.map((User user1) -> getPersonFromUser(user))

给定了一个 Lambda,它接受一个将 User 转换为 Person 的函数,因此我从拥有Mono for User"跳了过去.到人的单声道".

is given a Lambda that takes in a Function that converts a User to a Person, hence I jumped from having a "Mono for User" to a "Mono for Person".

我不知道是否有办法直接从 Person 获取 ServerResponse 但在第二个 map 调用中,您可以编写函数以返回 ServerResponse(在这种情况下,您有一个ServerResponse 的 Mono",您基本上就完成了)或返回另一个中间对象.如果您选择后者,您可能需要额外的映射调用.

I don't know if there is a way to get a ServerResponse directly from Person but in the second map call, you can write the function to return a ServerResponse (in which case you have a "Mono of a ServerResponse" and you're basically done) or return another intermediary object. If you do the latter, you'll likely need additional mapping calls.

注意:如果在您的 Lambda 中,您得到一个 Mono,您将需要使用 flatMap 方法,即如果 getPersonFromUser(user) 返回一个 Mono人而不是,然后代替

Note: If within your Lambda, you get a Mono, you'll want to use the flatMap method, i.e. if getPersonFromUser(user) returns a Mono of a Person instead of a Person, then instead of

.map((User user1) -> getPersonFromUser(user))

你会使用

.flatMap((User user1) -> getPersonFromUser(user))

如果您有兴趣,可以查看此StackOverFlow 问题mapflatmap

You can checkout this StackOverFlow Question if you're interested in map vs flatmap

这篇关于Spring-Webflux:在没有 block() 的情况下从 Mono 中提取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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