使用@Validated校验数据(除数据库做辅助)

一、controller层

	/**
	 * 	使用@Validated来进行校验
	 * @author HuangJingNa
	 * @date 2019年12月23日 下午6:02:20
	 *
	 * @param validatedTestParam
	 * @return
	 */
	@GetMapping("validated_test")
	@NotCheckOnline
	public Object validatedTest(@Validated ValidatedTestParam validatedTestParam) {
		return "校验完成";
	}

二、controller层传递的参数封装类

package cn.kooun.pojo.params;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 	通过注解校验数据
 * @author HuangJingNa
 * @date 2019年12月23日 下午6:41:00
 *
 */
@Getter
@Setter
@ToString
public class ValidatedTestParam {
	@NotBlank(message = "姓名不能为空")
	@Pattern(regexp = ".*{1,50}", message = "姓名不合法")
	private String name;
	@NotBlank(message = "学号不能为空")
	@Pattern(regexp = "[\w]{6,20}", message = "学号不合法")
	private String number;
	@NotNull(message = "年龄不能为空")
	@Min(value = 1, message = "年龄不合法")
	@Max(value = 200, message = "年龄不合法")
	private Integer age;
	@NotNull(message = "性别不能为空")
	@Min(value = 0, message = "性别不合法")
	@Max(value = 1, message = "性别不合法")
	private Integer sex;
}

三、全局异常处理

package cn.kooun.core.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kooun.common.result.Result;
import cn.kooun.common.result.ResultUtils;
import cn.kooun.pojo.exception.OffLineException;

/**
 *	全局异常处理
 * @author HuangJingNa
 * @date 2019年12月21日 下午3:46:19
 *
 */
@ControllerAdvice//标记此类为全局异常拦截器
public class GlobalExceptionHandler {
	/**
	 * 	系统异常处理,如404、500
	 * @author HuangJingNa
	 * @date 2019年12月21日 下午3:48:45
	 *
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value = Exception.class)//监听对应的异常对象
	@ResponseBody
	public Object defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception{
		//控制台输出错误信息
		e.printStackTrace();
		if(e instanceof OffLineException) {
			return ResultUtils.error("登录失效,请重新登录~", Result.JUMP_LOGIN);
		}
		return ResultUtils.error("系统繁忙,请联系管理员~");
	}
	/**
	 * 	自定义异常处理@Validated抛出的数据校验
	 * @author HuangJingNa
	 * @date 2019年12月21日 下午3:54:32
	 *
	 * @param req
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value = BindException.class)
	@ResponseBody
	public Object defaultArithmeticHandler(HttpServletRequest req, BindException e) throws Exception{
		//控制台输出错误信息
		e.printStackTrace();
		return ResultUtils.error(
				e.getBindingResult().getFieldError().getDefaultMessage(), 
    			"BindException");
	}
}

注:使用@Validated注解来校验数据,随机触发校验,不按从上往下的变量进行校验,所以提示也是乱的(可能是使用多线程来处理的)
且在此校验完成后,还要进入service层再进行有关数据库做辅助的数据校验,不利于用户体验,但是提高了开发效率

原文地址:https://www.cnblogs.com/nadou/p/14004397.html