尝试将我的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是否为空以及从中提取电话属性的最佳方法是什么?
答案 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的方法重写为响应式方法。我在下面给了他们一些评论和说明:
flatMap
来保持对创建的闭包内User
实体的访问。