这个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.*的一些注解在后台完成数据校验的更多相关文章

  1. 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 ...

  2. hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

    spring maven项目,使用hibernate validator 注解形式校验客户端的请求参数. hibernate-validator版本:5.0.2.Final validation-ap ...

  3. org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Integer

    使用hibernate时,在save方法时,报了:org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.In ...

  4. javax.validation.UnexpectedTypeException: No validator could be found for constraint 'org.hibernate.validator.constraints.Length' validating type

    使用hibernate validator出现上面的错误, 需要注意: @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  5. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  6. Hibernate validator使用和自定义validator及整合Spring MVC

    http://blog.csdn.net/lwphk/article/details/43983669 Hibernate validator使用 导入validation-api-xxx.jar 以 ...

  7. hibernate validator自定义校验注解以及基于服务(服务组)的校验

    hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...

  8. Spring Boot集成Hibernate Validator

    废话不多说,直接开始集成环境. 一.环境集成 在项目中hibernate-Validator包在spring-boot-starter-web包里面有,不需要重复引用 .(整个Demo都是用PostM ...

  9. spring boot 使用hibernate validator 验证service

    不在controller中验证,而是在service中验证. spring boot 默认使用的就是hibernate validator,存在于pom的spring-boot-starter-web ...

随机推荐

  1. 【7-10 PAT】树的遍历

    给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序列.第三 ...

  2. idea离线安装lombock插件

    技术交流群:233513714 安装过程 1.首先找到插件包 插件包可以在两个地方下载,分别是IDEA的官方插件仓库和GitHub里lombok-intellij-plugin仓库中的release包 ...

  3. CSP201604-1:折点计数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  4. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  5. linux备忘录-系统服务daemon

    服务(daemon)及其分类 Linux中的服务被称为daemon(daemon是守护神,恶鬼的意思哦).这些daemon会常驻在内存当中,从而对我们的系统和任务等进行一些辅助性的工作.实际上,dae ...

  6. sql 条件插入

    原普通插入语句: insert into seriestable_upload values(New.SeriesID,0); 加条件后:(当不存在该条数据插入) insert into custom ...

  7. Win10系统开启IIS服务步骤

    原文链接:http://www.111cn.net/sys/361/93003.htm

  8. 利用Github Pages建立仓库“门面”

    嘛是Github Pages? Github Pages 是一个静态网站托管服务,用来从你的Github仓库中直接发布 个人.组织或项目的网站页面 Github Pages发布的页面统一使用githu ...

  9. thinkphp include处理加载重复出现的html页面

    在网站设计经常用大量使用的html模板,例如header(一般是menu层甚至还带有图片轮播).left(后台管理页面比较常见的导航).footer(结尾层,一般也是小型的menu),使用includ ...

  10. SpringBoot2.0

    建立可执行的Jars和Wars bootJar用于构建可执行的Jar: bootWar用于构建可执行的war. application.properties 不启动web服务器 spring.main ...