验证输入并在Webflux中引发异常的最佳方法

时间:2019-06-13 08:54:40

标签: spring spring-webflux project-reactor

我在询问在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());
    });
}

感谢(:

0 个答案:

没有答案