1、首先创建一个测试实体类Person,并携带如上注解,其注解的作用描述在message
- package com.clickpaas.pojo;
- import com.fasterxml.jackson.annotation.JsonFormat;
- import lombok.Data;
- import org.hibernate.validator.constraints.Length;
- import org.hibernate.validator.constraints.Range;
- import javax.validation.constraints.*;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.List;
- /**
- * @author 方延杰
- * @version 1.0
- * @since 2020/12/10 9:04 下午
- */
- @Data
- public class Person {
- @Null(message = "death必须为null")
- private String death;
- @AssertTrue(message = "bool必须为true")
- private boolean bool;
- @AssertFalse(message = "fal必须为false")
- private boolean fal;
- @Min(value = 1, message = "min必须为数字,其值大于或等于指定的最小值")
- private Integer min;
- @Max(value = 10, message = "max必须为数字,其值小于或等于指定的最大值")
- private Integer max;
- @DecimalMin(value = "1", message = "minDeci最小不能小于1")
- private BigDecimal minDeci;
- @DecimalMax(value = "10", message = "maxDeci最大不能大于10")
- private BigDecimal maxDeci;
- @Size(min = 1, max = 2, message = "list集合的长度最小不能小于1,最大不能大于2")
- private List<Object> list;
- @Digits(integer = 4, fraction = 2, message = "digits整数位不能超过4个,小数位不能超过2个")
- private BigDecimal digits;
- /**
- * 将前台传来的日期数据映射到此字段
- */
- @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
- @Past(message = "past必须为过去的日期")
- private Date past;
- @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
- @Future(message = "future必须为将来的日期")
- private Date future;
- @Pattern(regexp = "^1[3|4|5|7|8][0-9]{9}$", message = "phone必须符合正则表达式")
- private String phone;
- @Email(message = "email必须是邮箱格式")
- private String email;
- @Length(min = 1, max = 2, message = "length长度最小不能小于1,最大不能大于2")
- private String length;
- @NotEmpty(message = "id不能为null,长度大于0")
- private String id;
- @Range(min = 1, max = 12, message = "month最小不能小于1,最大不能大于12")
- private Integer month;
- @NotBlank(message = "name不能为null,字段串长度大于0(限字符串)")
- private String name;
- }
2、封装返回响应体
- package com.clickpaas.response;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- /**
- * @author 方延杰
- * @version 1.0
- * @since 2019/7/3 4:32 下午
- */
- @Data
- @AllArgsConstructor
- public class CodeMsg {
- private int code;
- private String msg;
- /**
- * 失败
- */
- public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
- }
- package com.clickpaas.response;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- /**
- * @author 方延杰
- * @version 1.0
- * @since 2019/7/3 4:35 下午
- */
- @Data
- @AllArgsConstructor
- public class Result<T> {
- /**
- * 返回状态码 除200其余全部失败
- */
- private int code;
- /**
- * 返回信息 除success其余全部失败
- */
- private String msg;
- /**
- * 泛型数据
- */
- private T data;
- /**
- * 成功时返回的类型
- *
- * @param data 数据
- * @param <T> 泛型
- * @return 泛型数据
- */
- public static <T> Result<T> success(T data) {
- return new Result<>(200, "success", data);
- }
- public static <T> Result<T> fail(CodeMsg codeMsg) {
- return new Result<>(codeMsg);
- }
- private Result(CodeMsg codeMsg) {
- if (codeMsg == null) {
- return;
- }
- this.code = codeMsg.getCode();
- this.msg = codeMsg.getMsg();
- }
- }
3、创建使用增强器拦截并返回异常信息
- package com.clickpaas.config;
- import com.clickpaas.response.CodeMsg;
- import com.clickpaas.response.Result;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Objects;
- /**
- * @author 方延杰
- * @version 1.0
- * @since 2018/12/10 4:59 下午
- */
- @RestControllerAdvice
- public class GlobalExceptionInterceptor {
- @ExceptionHandler(value = MethodArgumentNotValidException.class)
- public Result<Object> exceptionHandler(HttpServletRequest request, Exception e) {
- String errMsg = "处理失败";
- if (e instanceof MethodArgumentNotValidException) {
- // 拿到参数校验具体异常信息
- errMsg = Objects.requireNonNull(((MethodArgumentNotValidException) e).getBindingResult().getFieldError()).getDefaultMessage();
- }
- return Result.fail(new CodeMsg(500, errMsg));
- }
- }
4、创建控制层测试
- package com.clickpaas.controller;
- import com.clickpaas.pojo.Person;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * @author 方延杰
- * @version 1.0
- * @since 2020/12/10 9:03 下午
- */
- @RestController
- public class ValidController {
- @PostMapping("/valid")
- public String valid(@Validated @RequestBody Person person) {
- return "success";
- }
- }
5、测试符合验证请求体访问,请求体如下:
- {
- "death":null,
- "bool":true,
- "fal":false,
- "min":1,
- "max":10,
- "minDeci":1,
- "maxDeci":10,
- "list":[
- {},{}
- ],
- "digits":1144.12,
- "past":"2020-10-01 10:00:00",
- "future":"2022-10-01 10:00:00",
- "phone":"15900445584",
- "email":"yanjie.fang@clickpaas.com",
- "length":"ab",
- "id":" ",
- "name":"a"
- }
6、故意修改不符合验证的数据
7、如果想做整体验证,如下:
- package com.clickpaas.uitl;
- import javax.validation.ConstraintViolation;
- import javax.validation.Validation;
- import javax.validation.Validator;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- /**
- * Bean整体校验工具类
- *
- * @author 方延杰
- * @version 1.0
- * @since 2020/12/10 10:08 下午
- */
- public class ValidatorUtils {
- private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
- /**
- * Bean整体校验,有不合规范,拼接message
- */
- public static String validate(Object obj, Class<?>... groups) {
- StringBuilder errorMessage = new StringBuilder();
- Set<ConstraintViolation<Object>> resultSet = VALIDATOR.validate(obj, groups);
- if (resultSet.size() > 0) {
- //如果存在错误结果,则将其解析并进行拼凑后异常抛出
- List<String> errorMessageList = resultSet.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
- errorMessageList.forEach(o -> errorMessage.append(o).append(";"));
- }
- return errorMessage.toString();
- }
- }
8、整体测试类
到此这篇关于详解Java后端优雅验证参数合法性的文章就介绍到这了,更多相关Java 验证参数合法性内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_48314739/article/details/111012090