Spring WebFlux-如何从请求中获取数据?

时间:2019-02-06 13:17:25

标签: spring-boot spring-webflux

尝试将我的Spring Boot应用程序迁移到WebFlux,我开始转换api层,同时保持存储库完整(即,数据库访问是同步和阻止的)。我面临着如何从Mono / Flux类型获取数据并将其转发到存储库的问题。

请考虑以下方法:

@POST
@Path("/register")
public String register( String body ) throws Exception
{
    ObjectMapper objectMapper = json();

    User user = objectMapper.readValue( body, User.class );

    int random = getRandomNumber( 111111, 999999 );

    String uuid = null;

    //first, check if user already did registration from that phone
    UserDbRecord userDbRecord = UserDAO.getInstance().getUserByPhone( user.phone );

    if( userDbRecord != null )
    {
        logger.info( "register. User already exist with phone: " + user.phone + ", id: " + userDbRecord.getId() );

        uuid = userDbRecord.getToken();
    }
    else
    {
        uuid = UUID.randomUUID().toString();
    }

    SMS.send( user.phone, random );

    Auth auth = new Auth();
    auth.token = uuid;

    return objectMapper.writeValueAsString( auth );
}

因此,请尝试执行以下操作:

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

    Mono<UserDbRecord> userDbRecord = user.flatMap( u -> Mono.just( userRepository.findByPhone( u.phone ) ) );

    int random = getRandomNumber( 111111, 999999 );

    String uuid = null;

    //first, check if user already did registration from that phone

    //now what???
    if( userDbRecord != null )
    {
        logger.info( "register. User already exist with phone: " + userDbRecord.getPhone() + ", id: " + userDbRecord.getId() );

        uuid = userDbRecord.getToken();
    }
    else
    {
        uuid = UUID.randomUUID().toString();
    }

    SMS.send( user.phone, random );

    Auth auth = new Auth();
    auth.token = uuid;

    return ok().contentType( APPLICATION_JSON ).syncBody( auth );
}

检查userDbRecord Mono是否为空以及从中提取电话属性的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

重新思考数据处理方式

在使用RxJava或Project Reactor进行反应式编程中,从头到尾继续进行流程确实很重要。

在这种情况下,您必须将命令式验证/检查替换为反应式的:

public Mono<ServerResponse> register( ServerRequest request )
{
    return request
        .bodyToMono( User.class )
        // make sure you use Reactive DataBase Access in order to 
        // get the all benefits of Non-Blocking I/O with Project Reactor
        // if you use JPA - consider Moving to R2DBC r2dbc.io
        .flatMap( user -> // <1>
            Mono.just( userRepository.findByPhone( user.phone ) ) // <2>
                .map(userDbRecord -> {
                    logger.info( "register. User already exist with phone: " + userDbRecord.getPhone() + ", id: " + userDbRecord.getId() );
                    return userDbRecord.getToken();
                })
                .switchIfEmpty(Mono.fromSupplier(() -> UUID.randomUUID().toString())) <3>
                .flatMap(uuid -> {
                    SMS.send( user.phone, random ); <4>
                    Auth auth = new Auth();
                    auth.token = uuid;
                    return ok().contentType( APPLICATION_JSON ).syncBody( auth );
                })
        );
}

以上示例显示了如何将命令式Controller的方法重写为响应式方法。我在下面给了他们一些评论和说明:

  1. 在这里我使用flatMap来保持对创建的闭包内User实体的访问。
  2. 确保从头到尾使用无阻塞的,反应性的I / O->不了解此规则可能导致否定WebFlux的所有优点。如果您使用JPA,请考虑迁移到R2DBC和Spring Data R2DBC,后者将为您提供JPA的反应性,无阻塞替代品
  3. 频繁生成UUID会导致线程阻塞-> https://stackoverflow.com/a/14533384/4891253
  4. 确保这是非阻塞性的