java中的Bean Validation怎么使用

42次阅读
没有评论

共计 3149 个字符,预计需要花费 8 分钟才能阅读完成。

本篇内容介绍了“java 中的 Bean Validation 怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

概念:java 中的 Bean Validation 是一个数据验证的规范。

说明:Hibernate Validator 是 Bean Validation 的一个具体实现。

举例:在 springMVC 中使用 Hibernate Validator

1)maven 依赖:dependency 
 groupId javax.validation /groupId 
 artifactId validation-api /artifactId 
 version 1.1.0.Final /version 
 /dependency 
 dependency 
 groupId org.hibernate /groupId 
 artifactId hibernate-validator /artifactId 
 version 5.1.2.Final /version 
 /dependency 
2)JavaBean:import javax.validation.constraints.NotNull;
 import org.hibernate.validator.constraints.NotBlank;
  *  在需要进行校验的属性上添加相应的注解: * 
  * 1)Bean Validation  提供的  constraint: 
  *  [@Null](https://my.oschina.net/u/561366)  被注释的元素必须为  null 
  * [@NotNull](https://my.oschina.net/notnull)  被注释的元素必须不为  null 
  * @AssertTrue  被注释的元素必须为  true 
  * [@AssertFalse](https://my.oschina.net/u/2430840)  被注释的元素必须为  false 
  * @Min(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值  
  * @Max(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值  
  * @DecimalMin(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值  
  * @DecimalMax(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值  
  * @Size(max=, min=)  被注释的元素的大小必须在指定的范围内  
  * @Digits (integer, fraction)  被注释的元素必须是一个数字,其值必须在可接受的范围内  
  * @Past   被注释的元素必须是一个过去的日期  
  * @Future   被注释的元素必须是一个将来的日期  
  * @Pattern(regex=,flag=)  被注释的元素必须符合指定的正则表达式  
  *  
  * 2)Hibernate Validator  特有的  constraint: * @NotBlank(message =) 验证字符串非 null,且长度必须大于 0  
  * @NotEmpty 被注释的字符串或集合的必须非空  
  * @Email  被注释的元素必须符合 email 的格式  
  * @Length(min=,max=) 被注释的字符串的大小必须在指定的范围内  
  * @Range(min=,max=,message=) 被注释的元素必须在合适的范围内  
  * 
  */ 
 public class User {
  
 @NotBlank(message =  name is null!)
 private String name;
 @NotNull(message =  age is null!)
 private Integer age;
 private String email;
 private String address;
 // getter and setter ..
3) 控制器:import javax.validation.Valid;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.ObjectError;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMethod;
 @RestController
 @RequestMapping(/sys)
 public class UserController {
  * 1. 给需要校验的参数添加  @Valid 注解  
  * 2. 给方法添加一个类型为  BindingResult 的参数,用来封装   校验的结果
  */
 @RequestMapping(value =  /user/add , method = RequestMethod.POST)
 public String addUser(@Valid @RequestBody User req, BindingResult bindingResult) {if (bindingResult.hasErrors()) {List ObjectError  allErrors = bindingResult.getAllErrors();
 List String  msgs = new ArrayList String 
 for (ObjectError objectError : allErrors) {String msg = objectError.getDefaultMessage();
 msgs.add(msg);
 String paramErrorMsg = StringUtils.join(msgs,      
 return paramErrorMsg;
 } else {
 System.out.println( do add user. 
 return  success! 
4) 接口测试:地址:http://localhost:8081/jxn-web/api/sys/user/add
 请求类型:Content-Type: application/json
 {name : , age :null} ==   响应内容:age is null!   name is null!
 {name : , age :} ==   响应内容:age is null!   name is null!
 {name : jack , age : 17} ==   响应内容:success!

常见错误:

 报错:javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.
分析:是由于 @NotBlank 修饰了 Integer、Long 等引用类型的属性
 @NotBlank
 private Integer age;
修正:应该使用 @NotNull 来修饰引用类型的属性。@NotNull
 private Integer age;

“java 中的 Bean Validation 怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计3149字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)