1.无法使用@NotBlank,@Max注解
pom.xml依赖升级一下
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
[email protected]无法验证问题
@Valid需要和Error类型连用,并且顺序不能改变,可以直接使用BindingResult,BindingResult继承了Error
@ApiOperation(value = "获取公告接口", notes = "获取公告", code = 200, produces = "application/json")
@RequestMapping(value = "/getnoticet",method= RequestMethod.POST,produces="application/json;charset=UTF-8")//url
public Result<List<NoticeResponse>>getnotice(@Valid NoticeRequest noticeRequest, BindingResult result){
System.out.println(result);
if (result.hasErrors()){
for (ObjectError error : result.getAllErrors()) {
return Result.<List<NoticeResponse>>builder().fail().code(500).message(error.getDefaultMessage()).build();
}
}
return noticetService.getnotice(noticeRequest);
}
另外,检查一下依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
hibernate-validator是为验证提供实现方法的,没有hibernate-validator就无法捕获