聊聊webflux参数校验

本文主要研究下webflux如何进行参数校验

使用javax.validation

domain

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

public class User {

    @Min(1)
    private long id;

    @NotEmpty
    private String name;

    @Min(20)
    private int age;
    //...
}

通过onErrorResume处理异常

  • form表单提交 /** * curl -i -X POST -d 'id=-1&name=a&age=10' http://localhost:8080/user/validate-mono * * curl -i -X POST -d 'id=2&name=a&age=10' http://localhost:8080/user/validate-mono * * curl -i -X POST -d 'id=2&name=a&age=30' http://localhost:8080/user/validate-mono * @param user * @return */ @PostMapping("/validate-mono") public Mono<String> formValidate(@Validated Mono<User> user) { return user .map(u -> "Hello " + u.getName()) .onErrorResume(WebExchangeBindException.class, e -> Mono.just(bindingResult2String(e.getBindingResult()))); }
  • post json /** * curl -i -H "Content-Type: application/json" -X POST -d '{"id":-11,"name":"a"}' http://localhost:8080/user/validate-mono-json-body * * curl -i -H "Content-Type: application/json" -X POST -d '{"id":12,"name":"a","age":30}' http://localhost:8080/user/validate-mono-json-body * @param user * @return */ @PostMapping(value = "/validate-mono-json-body",produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public Mono<String> requestBodyValidate(@Validated @RequestBody Mono<User> user) { return user .map(u -> "Hello " + u.getName()) .onErrorResume(WebExchangeBindException.class, e -> Mono.just(bindingResult2String(e.getBindingResult()))); } 注意这里catch住WebExchangeBindException

通过ExceptionHandler处理WebExchangeBindException

  • ExceptionHandler @ExceptionHandler(WebExchangeBindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public String handleBindException(WebExchangeBindException bindException) { return bindingResult2String(bindException.getBindingResult()); }
  • controller @PostMapping(value = "/validate-raw-json-body",produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public Mono<String> validateRawReqBody(@RequestBody @Validated User user /**,BindingResult bindingResult**/) { //注意在参数添加BindingResult bindingResult会报错,还是交给ExceptionHandler那里处理 return Mono.just("Hello " + user.getName()); } 注意这里没有使用onErrorResume来处理WebExchangeBindException,而是交给ExceptionHandler处理

手工校验

javax.validation虽然是方便,但是具体业务场景非常复杂,不是简单的单个字段校验就完事的,有的需要关联校验,这个时候javax.validation就无能为力了。这个时候可能就需要手工来写校验。

    /**
     * curl -i -X POST -d 'id=-1&name=a&age=10' http://localhost:8080/user/manual-validate
     *
     * curl -i -X POST -d 'id=2&name=a&age=-1' http://localhost:8080/user/manual-validate
     * @param user
     * @return
     */
    @PostMapping(value = "/manual-validate",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Mono<String> manualValidate(Mono<User> user){
        return user.flatMap(u -> {
            if(u.getAge() < 0){
                return Mono.error(new RuntimeException("age < 0"));
            }else{
                return Mono.just("success");
            }
        });
    }

小结

通过使用javax.validation自动校验及Mono.error手工校验,基本可以处理大多数webflux参数校验场景。

doc

  • Webflux-Validation
  • How to validate Mono when using Spring Reactive

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-03-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区