Spring参数校验 @Validated @RequestParam

直接上才艺,代码可以Copy进行测试,楼下还有效果图


import java.util.HashMap;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
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 org.hibernate.validator.constraints.Length;
import org.springframework.validation.BindException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RestControllerAdvice//捕获接口异常
@RequestMapping("test")
@Validated//直接使用方法接受参数验证 @Validated 加在接口类上
public class ValidationController {

	/**校验日期正则*/
	private final static String REGEXP = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))";

	/**
	 * 直接用方法进行接收参数
@Validated 加在接口类上 * @param username * @param password * @param age * @param date * @param request * @return */ @RequestMapping("t1") public Object t1(@NotNull(message = "用户名为空") @Length(max = 8, min = 5, message = "登入账号5~8位") String username, @NotBlank(message = "密码为空") @Length(max = 8, min = 5, message = "密码长度5~8位") String password, @Max(value = 99, message = "最大为99") @Min(value = 18, message = "最小为18") Integer age, @Pattern(regexp = REGEXP, message = "日期格式yyyy-MM-dd") String date, HttpServletRequest request) { log.info("t1.ok");//参数验证通过 return request.getParameterMap(); } /** * 用对象方式接受参数 * @param user * @return */ @RequestMapping("t2") public Object t2(@Validated User user) { log.info("t2.ok");//参数验证通过 return user; } @Data static class User { @NotNull(message = "用户名为空") @Length(max = 8, min = 5, message = "登入账号5~8位") String username; @NotBlank(message = "密码为空") @Length(max = 8, min = 5, message = "密码长度5~8位") String password; @Max(value = 99, message = "最大为99") @Min(value = 18, message = "最小为18") Integer age; @Pattern(regexp = REGEXP, message = "日期格式yyyy-MM-dd") String date; } /** * 对象方式参数校验失败- t2 方法验证失败进入 * @param exception * @return */ @ExceptionHandler(value = BindException.class) public Object hadeConstraintViolationException(BindException exception) { log.info("BindException .... "); HashMap errors = new HashMap<>(); exception.getFieldErrors().forEach(f->{ errors.put(f.getField(),f.getDefaultMessage() ); }); return errors; } /** * 直接用方法进行接收参数校验失败- t1 方法验证失败进入 * @param exception * @return */ @ExceptionHandler(value = ConstraintViolationException.class) public Object hadeConstraintViolationException(ConstraintViolationException exception) { log.info("ConstraintViolationException .... "); HashMap errors = new HashMap<>(); Set> violations = exception.getConstraintViolations(); String message = exception.getMessage(); System.err.println("message = " + message); violations.forEach(c -> { String val = c.getMessage(); String key = c.getPropertyPath().toString(); errors.put(key, val); System.err.println(c); }); return errors; } }

效果图

Spring参数校验 @Validated @RequestParam_第1张图片

你可能感兴趣的:(JAVA,java,参数校验,Validated)