使用 mongo 反应式 spring 检查 Webflux 数据
Webflux data check with mongo reactive spring
我尝试学习 Webflux,但是当我想在保存数据之前验证员工的列表 ID 时遇到问题。
我的问题
当 employeeId 不存在时如何捕获错误并将错误显示给客户端?
@PostMapping(path = "/{tenantId}/outlet")
public Mono<OutletEntity> createNewOutlet(@PathVariable String tenantId, @RequestBody OutletEntity outletEntity) {
return Mono.just(outletEntity).map(outletEntity1 -> {
outletEntity.getEmployees().forEach(s -> {
this.employeeService.getRepository().existsById(s).subscribe(aBoolean -> {
System.out.println(aBoolean);
if (!aBoolean) {
/**
* variable s is employeId
* i want to validate every single employee id before save new outlet
*/
throw new ApiExceptionUtils("tenant not found", HttpStatus.UNPROCESSABLE_ENTITY.value(),
StatusCodeUtils.TENANT_NOT_FOUND);
}
});
});
return outletEntity1;
}).flatMap(outletEntity1 -> {
outletEntity.setTenantId(tenantId);
return this.outletRepository.save(outletEntity);
});
更好的方法运行您在同一条链中进行验证而无需额外的订阅者
return Flux.fromIterable(outletEntity.getEmployees()) (1)
.flatMap(this.employeeService.getRepository()::existsById)
.doOnNext(System.out::println)
.map(aBoolean -> {
if (!aBoolean) { (2)
throw new ApiExceptionUtils("tenant not found", HttpStatus.UNPROCESSABLE_ENTITY.value(),
StatusCodeUtils.TENANT_NOT_FOUND);
}
else {
return aBoolean;
}
})
.then(Mono.just(outletEntity)) (3)
.flatMap(outletEntity1 -> {
outletEntity.setTenantId(tenantId);
return this.outletRepository.save(outletEntity);
});
1) 从员工集合创建 Flux,并通过反应器流进行验证迭代;
2) 检查您的类型是否为 false 并抛出异常,它会停止此链;
3) 如果一切 运行 顺利 then() 使用 outletEntity 切换到 Mono,保存它并 returns;
关于错误处理。
如果您不处理错误,WebFlux 会在 DefaultErrorWebExceptionHandler.
中解决它
您可以像在 Web MVC 中那样添加自己的错误处理,或者在 WebFlux Config.
中添加自定义异常处理程序
您可以在此处阅读更多详细信息:web-reactive
我尝试学习 Webflux,但是当我想在保存数据之前验证员工的列表 ID 时遇到问题。 我的问题 当 employeeId 不存在时如何捕获错误并将错误显示给客户端?
@PostMapping(path = "/{tenantId}/outlet")
public Mono<OutletEntity> createNewOutlet(@PathVariable String tenantId, @RequestBody OutletEntity outletEntity) {
return Mono.just(outletEntity).map(outletEntity1 -> {
outletEntity.getEmployees().forEach(s -> {
this.employeeService.getRepository().existsById(s).subscribe(aBoolean -> {
System.out.println(aBoolean);
if (!aBoolean) {
/**
* variable s is employeId
* i want to validate every single employee id before save new outlet
*/
throw new ApiExceptionUtils("tenant not found", HttpStatus.UNPROCESSABLE_ENTITY.value(),
StatusCodeUtils.TENANT_NOT_FOUND);
}
});
});
return outletEntity1;
}).flatMap(outletEntity1 -> {
outletEntity.setTenantId(tenantId);
return this.outletRepository.save(outletEntity);
});
更好的方法运行您在同一条链中进行验证而无需额外的订阅者
return Flux.fromIterable(outletEntity.getEmployees()) (1)
.flatMap(this.employeeService.getRepository()::existsById)
.doOnNext(System.out::println)
.map(aBoolean -> {
if (!aBoolean) { (2)
throw new ApiExceptionUtils("tenant not found", HttpStatus.UNPROCESSABLE_ENTITY.value(),
StatusCodeUtils.TENANT_NOT_FOUND);
}
else {
return aBoolean;
}
})
.then(Mono.just(outletEntity)) (3)
.flatMap(outletEntity1 -> {
outletEntity.setTenantId(tenantId);
return this.outletRepository.save(outletEntity);
});
1) 从员工集合创建 Flux,并通过反应器流进行验证迭代;
2) 检查您的类型是否为 false 并抛出异常,它会停止此链;
3) 如果一切 运行 顺利 then() 使用 outletEntity 切换到 Mono,保存它并 returns;
关于错误处理。 如果您不处理错误,WebFlux 会在 DefaultErrorWebExceptionHandler.
中解决它您可以像在 Web MVC 中那样添加自己的错误处理,或者在 WebFlux Config.
中添加自定义异常处理程序您可以在此处阅读更多详细信息:web-reactive