SSM使用hibernate-validator
这里记录一下,SSM项目使用hibernate-validator,因为和SpringBoot项目使用,还是有一定的差异的。
引入依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.api.version}</version>
</dependency>
配置文件
<!-- 国际化配置 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 注册验证器 -->
<mvc:annotation-driven validator="validator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!--这里配置将使用上面国际化配置的messageSource -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
实体类
/**
* customer
* @author
*/
public class Customer implements Serializable {
/**
* 客户编号(主键)
*/
private Integer custId;
/**
* 客户名称
*/
@NotBlank(message = "客户名不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custName;
/**
* 负责人id
*/
private Integer custUserId;
/**
* 创建人id
*/
private Integer custCreateId;
/**
* 客户信息来源
*/
@NotBlank(message = "客户信息来源不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custSource;
/**
* 客户所属行业
*/
@NotBlank(message = "客户所属行业来源不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custIndustry;
/**
* 客户级别
*/
@NotBlank(message = "客户级别不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custLevel;
/**
* 联系人
*/
@NotBlank(message = "联系人不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custLinkman;
/**
* 固定电话
*/
@NotBlank(message = "固定电话不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custPhone;
/**
* 移动电话
*/
@NotBlank(message = "移动电话不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custMobile;
/**
* 邮政编码
*/
@NotBlank(message = "邮政编码不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custZipcode;
/**
* 联系地址
*/
@NotBlank(message = "联系地址不能为空", groups = {CustomerGroup.CreateCustomer.class})
private String custAddress;
/**
* 创建时间
*/
private Date custCreatetime;
private static final long serialVersionUID = 1L;
/**
* Getter Setter方法
*/
}
分组类
public interface CustomerGroup {
interface CreateCustomer{};
}
Controller的使用
@RequestMapping(value = "/customer/create.action",method = RequestMethod.POST)
@ResponseBody
public Result create(@Validated(CustomerGroup.CreateCustomer.class) @RequestBody Customer customer, BindingResult result){
if(result.hasErrors()){
List<String> errs = new ArrayList<>();
result.getFieldErrors().forEach(f->errs.add(f.getDefaultMessage()));
return Message.createFail(errs);
}
return customerService.insertSelective(customer);
}
因为我这一直以为和SpringBoot项目使用一样,不用配置什么,还有用@ControllerAdvice错误捕捉器捕捉抛出的错误,但是一直是不符合校验就返回400响应码,纳闷了很久,后面才知道,只能在Controller接受错误去判断
一键查询淘宝/拼多多内部优惠券,每日大额外卖红包,购物省钱的宝藏工具