後臺驗證的做用主要是防止postman...等等工具的惡意提交,先後臺都判斷數據,雙保險.app
1.能夠在SpringBoot傳參數 加上NotNull.... //分組Default,分組的好處就是可重複使用. public interface Default{} //Update public interface Update{} /** * 編號. */ @NotNull(message = "id不能爲空",groups = Update.class) private Integer id; /** * 狀態碼 */ @NotBlank(message = "請輸入狀態碼",groups = Default.class) private String type_code; /** * 狀態的值 */ @NotBlank(message = "請輸入狀態值",groups = Default.class) private String type_key; /** * 狀態值,前臺展現的值 */ @NotBlank(message = "請輸入字典表顯示值",groups = Default.class) private String type_value; /** * 備註 */ private String remark; 2.在類上這麼寫: @RequestMapping("/index") public String index(@RequestBody @Validated(value = Testform.Default.class) Testform wordbookform, BindingResult bindingResult){ if(bindingResult.hasErrors()){//錯誤: return bindingResult.getFieldError().getDefaultMessage(); } return "index"; } @RequestMapping("/index02") public String index02(@RequestBody @Validated(value = Testform.Update.class) Testform wordbookform, BindingResult bindingResult){ if(bindingResult.hasErrors()){ return bindingResult.getFieldError().getDefaultMessage(); } return "index"; }