spring3 mvc注解校验@NotNull不起作用,该怎么解决
2个回答
展开全部
有可能是pom导入包错误,@NotNull没有生效,没有验证,进入了数据库就会报500的错误
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
//创建
@RequestMapping(path = "/", method = RequestMethod.POST)
public UserDto createUser(@Valid @RequestBody UserDto userDto) {
return userService.createUser(userDto);
}
@NotNull(message="用户名不能为空")
private String nickName;
如果是生效的注解,会报400的错误
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
//创建
@RequestMapping(path = "/", method = RequestMethod.POST)
public UserDto createUser(@Valid @RequestBody UserDto userDto) {
return userService.createUser(userDto);
}
@NotNull(message="用户名不能为空")
private String nickName;
如果是生效的注解,会报400的错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
配置好注解扫描
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询