order
This paper mainly studies how to verify the parameters of webplus
Using 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; //... }
Handling exceptions through onErrorResume
- Form form submission
/** * 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()))); }
Notice that here catch lives in WebExchangeBindException
Handling WebExchangeBindException with ExceptionHandler
- ExceptionHandler
@ExceptionHandler(WebExchangeBindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public String handleBindException(WebExchangeBindException bindException) { return bindingResult2String(bindException.getBindingResult()); }
- from form submission
/** * 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()); }
Note that we need to mark "produces = mediatype. Application" JSON "utf8" value, otherwise it will report 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()); }
Note that the onErrorResume is not used to handle the WebExchangeBindException here, but is handed over to the ExceptionHandler
Manual calibration
Although javax.validation is convenient, the specific business scenario is very complex. It is not a simple single field verification. Some need to be associated with the verification. At this time, javax.validation is powerless. You may need to write the verification manually at this time.
/** * 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"); } }); }
Summary
By using javax.validation automatic verification and Mono.error manual verification, you can basically handle most weblux parameter verification scenarios.