{ "timestamp":"2020-01-06T03:03:18.125+0000", "status":500, "error":"Internal Server Error", "message":"Maximum value of score is 100, Student gender is mandatory", "path":"/student" }
同时,控制台里出现了这样的日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2020-01-06 11:21:25.971 INFO 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Validating bean with validator org.hibernate.validator.internal.engine.ValidatorImpl 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Error message: Student gender is mandatory 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Property path: gender 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Error value: null 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Error message: Maximum value of score is 100 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Property path: score 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : Error value: 180 2020-01-06 11:21:26.214 ERROR 68021 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:21:26.252 ERROR 68021 --- [nio-9999-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Student gender is mandatory, Maximum value of score is 100] with root cause
java.lang.RuntimeException: Student gender is mandatory, Maximum value of score is 100 at com.example.demo.StudentController.showStudent(StudentController.java:42) ~[classes/:na] 堆栈信息太多,下面的略掉了
看来,校验的代码成功起作用了。
简化代码
因为我们现在基本上都是面向 Spring 编程,所以其实上面那些手动获取 Validator 的代码也是不必要的。我们可以让 Spring 自动注入一个 Validator 来实现功能。
2020-01-06 11:15:17.957 INFO 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Validating bean with validator org.springframework.validation.beanvalidation.LocalValidatorFactoryBean 2020-01-06 11:15:18.071 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Error message: Student gender is mandatory 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Property path: gender 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Error value: null 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Error message: Maximum value of score is 100 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Property path: score 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : Error value: 180 2020-01-06 11:15:18.072 ERROR 67745 --- [nio-9999-exec-1] com.example.demo.StudentController : ======================= 2020-01-06 11:15:18.089 ERROR 67745 --- [nio-9999-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Student gender is mandatory, Maximum value of score is 100] with root cause
java.lang.RuntimeException: Student gender is mandatory, Maximum value of score is 100 at com.example.demo.StudentController.showStudent(StudentController.java:42) ~[classes/:na] 下面的堆栈信息依旧略掉