聊聊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()); }
- from表单提交
/** * 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(value = "/validate-mono",produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Mono<String> formValidate(@Validated Mono<User> user) { return user .map(u -> "Hello " + u.getName()); }注意这里要标注produces = MediaType.APPLICATION_JSON_UTF8_VALUE,否则报406 Not Acceptable
- 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,而是交给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
相关推荐
阳光岛主 2020-06-01
阿债的方寸天地 2020-04-29
liwf 2020-11-10
sjun0 2020-11-12
做对一件事很重要 2020-09-07
BraveWangDev 2020-08-19
songshijiazuaa 2020-08-15
FanErZong 2020-07-18
大慧 2020-07-04
houjinkai 2020-06-18
LUOPING0 2020-06-16
Carlos 2020-06-16
85427617 2020-06-13
心丨悦 2020-06-13
guicaizhou 2020-06-12
wenjieyatou 2020-06-08
stoneechogx 2020-06-04