参数校验
电脑版发表于:2021/12/1 16:24
转载 https://blog.csdn.net/hg2726028/article/details/78048502
1.Validator的jar包依赖
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>5.4.1.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.0.Final</version> </dependency>
2.一般在conroller层中用对象接受参数信息时,会对参数进行校验,为了简化代码,hibernate validator提供了基于注解方式的参数校验
package com.user.vo; import com.alibaba.fastjson.JSON; import org.hibernate.validator.constraints.NotEmpty; /** * 注册对象实例类 * * @author CaiWei * @version 1.0 * @created 2017-09-12 23:44:26 * @since 1.0 **/ public class RegisterUserBean { @NotEmpty(message = "用户名不可为空") private String userName; private String nickName; @NotEmpty(message = "密码不可为空") private String passWord; @NotEmpty(message = "确认密码不可为空") private String confirmPwd; private String sex; private String mobile; }
3.controller层加入@valid注解
package com.user; import com.alibaba.fastjson.JSON; import com.base.ResponseEntity; import com.user.entity.UserEntity; import com.user.service.IUserService; import com.user.vo.RegisterUserBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.validation.Valid; import java.util.List; /** * something to description this class * * @author CaiWei * @version 1.0 * @created 2017-09-12 23:33:01 * @since 1.0 **/ @Controller @RequestMapping(value = "/user/register") public class UserRegisterController { private final Logger LOGGER = LoggerFactory.getLogger(UserRegisterController.class); @Autowired private IUserService userService; @RequestMapping(value = "/page") public String toRegisterPage() { return "/user/register"; } @RequestMapping(value = "/save",method = RequestMethod.POST) public ResponseEntity registered(@Valid RegisterUserBean userBean, BindingResult bindingResult) { LOGGER.info("注册的对象为:{}", JSON.toJSONString(userBean)); if (bindingResult.hasErrors()){ List<FieldError> errors = bindingResult.getFieldErrors(); for (FieldError error :errors){ LOGGER.info(error.getDefaultMessage()); } return new ResponseEntity().error(errors.get(0).getDefaultMessage()); } boolean res = false; if (!"{}".equals(JSON.toJSONString(userBean))) { UserEntity usr = new UserEntity(); usr.setUsername(userBean.getUserName()); usr.setCreatetime(System.currentTimeMillis() / 1000); usr.setNickname(userBean.getNickName()); usr.setMobile(userBean.getMobile()); usr.setUsertype(0); usr.setPassword(userBean.getConfirmPwd()); res = userService.save(usr); } return res ? new ResponseEntity().success() : new ResponseEntity().error(); } }