@RestController @Validated class Controller { // ... }
爲Controller添加@Validated
表示啓用驗證。正則表達式
@GetMapping public ResponseEntity<List<Permission>> query(@RequestHeader("iv-user") String userId, @Pattern(regexp = "^accessor$", message = "請求參數不合法") @RequestParam("findBy") String accessor, @RequestParam("accessorId") long accessorId){ //... }
爲Controller中特定請求添加@Pattern(regexp = "^accessor$", message = "請求參數不合法")
正則表達式校驗。spring
{ "timestamp": "2018-07-19T02:08:01.372+0000", "status": 500, "error": "Internal Server Error", "message": "query.accessor: 請求參數不合法", "path": "/budget/permissions" }
很方便,還能夠本身捕獲這個異常的對返回進行自定義處理。app
Spring Boot REST @RequestParam not being Validated spring-boot