在利用springboot编写服务器代码的时候,我们对于一个pojo类(可能是前端传过来的)一定要进行参数验证。如果写大量的参数检验方法,也是一件很麻烦事。那么,请往下看......
1.创建工具类
package com.hierway.vslm.common.util;
import com.hierway.utility.api.result.ApiException;
import com.hierway.utility.api.result.ResultCode;
import org.hibernate.validator.HibernateValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
/**
* @ClassName ValidateUtil
* @Description 验证工具类
* @Date 2019/8/12 17:36
* @Created by sunyiwei
*/
public class ValidateUtil {
/**
*
* @param validateData
* @param resultCode
* @param validateGroup
* @param <T>
*/
public static <T> void validateParams(T validateData, ResultCode resultCode, Class... validateGroup){
if(validateData == null){
throw new ApiException(ResultCode.PARAMS_ERROR);
}
Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();
Set<ConstraintViolation<T>> validate = validator.validate(validateData,validateGroup);
for(ConstraintViolation<T> error : validate){
throw new ApiException(resultCode,error.getMessage());
}
}
}
其中ApiException为自定义的异常,我们也可以换成return。
2.pojo类中编写验证注解以及错误提示(注解请百度)
package com.hierway.vslm.dataaccess.mybatis.dao;
import com.hierway.model.vslm.common.ApsTime;
import com.hierway.model.vslm.stream.StreamLine;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;
import java.util.Objects;
import static com.hierway.vslm.common.util.TimeUtil.splitTime;
/**
* @ClassName StreamLine
* @Description 流水线与数据库映射实体类, 以下属性如果与数据库字段一样,或者属性驼峰转下划线后与数据库一致,则不需要定义@Colomn
* 如果实体类名与数据库一致,或大驼峰转下划线与数据库表名一致,则不需要定义@Table注解
* @Date 2019/7/16 16:16
* @Created by sunyiwei
*/
public class SLine {
@Id
private String lineId;
@Pattern(regexp = "[\\u4e00-\\u9fa5]*[a-z]*[A-Z]*\\d*-*_*", message = "Illegal name")
@NotBlank(message = "name is empty")
private String name;
private String area;
@NotNull(message = "spec is empty")
private String specId;
@NotNull(message = "vplId is empty")
@NotBlank(message = "vpl No. is blank")
private String vplId;
private Integer status;
@NotNull(message = "startTime is empty")
private LocalDateTime startTime;
@NotNull(message = "endTime is empty")
private LocalDateTime endTime;
private LocalDateTime createTime;
private LocalDateTime modifyTime;
private String modifyPerson;
public SLine() {
}
}
3.使用
package com.hierway.vslm.controller;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import static com.hierway.vslm.common.util.ValidateUtil.*;//静态导包
import java.util.List;
@RestController
public class StreamLineRestApi {
@Autowired
private StreamLineService streamLineService;
@ApiOperation(value = "")
@RequestMapping(value = "/sline",method = RequestMethod.POST)
public ApiResult<Void> createStreamLine(@RequestBody SLine streamLine){
validateParams(streamLine,ResultCode.PARAMS_ERROR);//此处调用验证方法,如果异常,则进入全局异常捕获
boolean createResult = streamLineService.createStreamLine(streamLine);
if(createResult == true){
return ResultUtil.success();
}
return ResultUtil.error(ResultCode.ADD_FAILED);
}
}