我在询问在webflux中验证数据并抛出异常的最佳方法,我编写了2个简单的代码示例来模拟这种情况
@GetMapping("/length")
public Mono<Integer> getLenght(Mono<String>nameMono)
{
return nameMono.map(name->
{
if(someValidationFailed(name))
{
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
return name.length();
});
}
和以下代码
@GetMapping("/length")
public Mono<Integer> getLenght(Mono<String>nameMono)
{
return nameMono.flatMap(name->
{
if(someValidationFailed(name))
{
Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST));
}
return Mono.just(name.length());
});
}
感谢(: