我正在学习使用 Reactor 进行反应式编程,我想实现注册场景,其中用户可以将多个帐户分配给同一配置文件。但是,分配给配置文件的用户名和分配给帐户的电话必须是唯一的。
正如您在下面的代码片段中看到的,如果 Reactor 提供了操作符,那么这个场景将很容易实现switchIfNotEmpty。
public Mono<PersonalAccountResponse> createPersonalAccount(PersonalAccountRequest request) {
return Mono
.just(request.isAlreadyUser())
.flatMap(isAlreadyUser -> {
if(isAlreadyUser){
return profileDao
.findByUsername(request.getUsername()) //
.switchIfEmpty(Mono.error(() -> new IllegalArgumentException("...")));
}else{
return profileDao
.findByUsername(request.getUsername())
.switchIfEmpty(Mono.from(profileDao.save(profileData)))
.switchIfNotEmpty(Mono.error(() -> new IllegalArgumentException("...")));
}
})
.map(profileData -> personalAccountMapper.toData(request))
.flatMap(accountData -> personalAccountDao
.retrieveByMobile(request.getMobileNumber())
.switchIfEmpty(Mono.from(personalAccountDao.save(accountData)))
.switchIfNotEmpty(Mono.error(() -> new IllegalArgumentException("..."))))
.map(data -> personalAccountMapper.toResponse(data, request.getUsername()));
}
Run Code Online (Sandbox Code Playgroud)
如果没有,我怎样才能实现这个要求switchIfNotEmpty?
谢谢
我使用 Spring 框架和 bean 验证规范(hibernate 实现)。考虑下面的代码片段,我使用以下方法进行了观察@ReportAsSingleViolation:
@ReportAsSingleViolation添加时仅返回默认消息。我的期望是触发失败的消息而不是默认消息。
当我删除@ReportAsSingleViolation注释时,一切都会按预期进行。与违反的约束相对应的所有错误消息都会正确返回。
此行为描述了 Bean 验证规范的一部分还是一个实现问题?
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
//@ReportAsSingleViolation
@NotBlank(message = "The input cannot be blank")
@Pattern(regexp = "[0-9A-Z_]+", message = "The input must content only uppercase, numbers and undescore")
@Size(max = 30, message = "The input must be maximal 30")
@Documented
public @interface MyAnnotation{
String message() default "{com.example.MyAnnotation.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Run Code Online (Sandbox Code Playgroud)