用spring的@Validated注解和org.hibernate.validator.constraints.*的一些注解在后台完成数据校验
这个demo主要是让spring的@Validated注解和hibernate支持JSR数据校验的一些注解结合起来,完成数据校验。这个demo用的是springboot。
首先domain对象Foo的代码如下:
package com.entity; import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank; import javax.validation.constraints.Min; public class Foo {
@NotBlank(message = "此name不能为空")
private String name;
@Min(value = ,message = "此age最小为18")
private Integer age;
@Email(message = "邮箱格式错误")
private String email; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}
其次controller层的代码如下:
package com.validation; import com.entity.Bar;
import com.entity.BarValidator;
import com.entity.Foo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid;
import java.util.List; @RestController
public class FooController { @RequestMapping("/fooTest")
public String foo(@Valid Foo foo, BindingResult result){
if(result.hasErrors()){
List<ObjectError> allErrors = result.getAllErrors();
for(ObjectError objectError :allErrors){
String defaultMessage = objectError.getDefaultMessage();
System.out.println("defalutMessage="+defaultMessage);
}
System.out.println("allErrors="+allErrors.toArray());
return "failed";
}
return "success";
} }
启动spring boot的代码如下:
package com.validation; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
用spring的@Validated注解和org.hibernate.validator.constraints.*的一些注解在后台完成数据校验的更多相关文章
- spring boot 1.4默认使用 hibernate validator
spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能.hibernate validator 5.2.4 Final是JSR 349 Be ...
- hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException
spring maven项目,使用hibernate validator 注解形式校验客户端的请求参数. hibernate-validator版本:5.0.2.Final validation-ap ...
- org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Integer
使用hibernate时,在save方法时,报了:org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.In ...
- javax.validation.UnexpectedTypeException: No validator could be found for constraint 'org.hibernate.validator.constraints.Length' validating type
使用hibernate validator出现上面的错误, 需要注意: @NotNull 和 @NotEmpty 和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...
- Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例
Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...
- Hibernate validator使用和自定义validator及整合Spring MVC
http://blog.csdn.net/lwphk/article/details/43983669 Hibernate validator使用 导入validation-api-xxx.jar 以 ...
- hibernate validator自定义校验注解以及基于服务(服务组)的校验
hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...
- Spring Boot集成Hibernate Validator
废话不多说,直接开始集成环境. 一.环境集成 在项目中hibernate-Validator包在spring-boot-starter-web包里面有,不需要重复引用 .(整个Demo都是用PostM ...
- spring boot 使用hibernate validator 验证service
不在controller中验证,而是在service中验证. spring boot 默认使用的就是hibernate validator,存在于pom的spring-boot-starter-web ...
随机推荐
- Node.js中的不安全跳转如何防御详解
Node.js中的不安全跳转如何防御详解 导语: 早年在浏览器大战期间,有远见的Chrome认为要运行现代Web应用,浏览器必须有一个性能非常强劲的Java引擎,于是Google自己开发了一个高性能的 ...
- xpath简单入门
语法: 选取节点: 实例: (贴图转载自w3school) 补充: /a/@href #获取a标签的href属性 当<div class="demo">& ...
- vue 项目如何使用微信分享接口
首先做微信网页都要接入微信sdk: 安装sdk npm install weixin-js-sdk --save 具体可以查看微信公众平台技术文档:https://mp.weixin.qq.com/w ...
- Linux下中文乱码问题
记录一下配置centos的时候遇到的一些常见问题 写了一个python脚本,有中文注释,而且会输出一些用户名称,其中包含中文字符.显示的时候出现乱码. 解决方案: 参见博客: Linux基础:中文显示 ...
- 最新cloudera大数据培训班 ccah ccdh 数据分析师 数据科学家
上海2月21-24日Cloudera Developer training for Spark and Hadoop(CCA-175)北京2月23-26日Cloudera Developer tr ...
- Django-Content-type用法
from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...
- mysql数据库----Pymysql
本节重点: pymysql下载和使用 sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetchall 一.pymysql的下载和使用 之前我们都是通过M ...
- 12-Mysql数据库----多表查询
本节重点: 多表连接查询 符合条件连接查询 子查询 准备工作:准备两张表,部门表(department).员工表(employee) create table department( id int, ...
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon ...
- 前端JQuery中获取一个div下的多个id值
获取所有的Id值,方法是通过div.class获取全局的值,然后再提取具体的Id值 方法一:用for循环,因为$("div.class")获取的是一个数组,通过循环读取出数组中的每 ...