自己网站建设,丰台网站建设,桑基图在线制作网站,什么是wordpress程序后端在处理前端传过来的数据时#xff0c;尽管前端表单已经加了校验逻辑#xff0c;但是作为严谨考虑#xff0c;在后端对接口传输的数据做校验也必不可少。 开启校验#xff1a; 实体类上增加校验注解#xff0c;接口参数前增加Valid 开启校验
package com.xxh.product.…后端在处理前端传过来的数据时尽管前端表单已经加了校验逻辑但是作为严谨考虑在后端对接口传输的数据做校验也必不可少。 开启校验 实体类上增加校验注解接口参数前增加Valid 开启校验
package com.xxh.product.entity;import javax.validation.constraints.*;
import org.hibernate.validator.constraints.URL;/*** 品牌** author xxh*/
Data
TableName(pms_brand)
public class BrandEntity implements Serializable {private static final long serialVersionUID 1L;/*** 品牌id*/NotNull(message 修改必须指定品牌id, groups {UpdateGroup.class})Null(message 新增不能指定id, groups {AddGroup.class})TableIdprivate Long brandId;/*** 品牌名*/NotBlank(message 品牌名必须提交, groups {AddGroup.class, UpdateGroup.class})private String name;/*** 品牌logo地址*/NotBlank(groups {AddGroup.class})URL(message logo必须是一个合法的url地址, groups {AddGroup.class, UpdateGroup.class})private String logo;/*** 介绍*/private String descript;/*** 显示状态[0-不显示1-显示]*/
// Pattern()NotNull(groups {AddGroup.class, UpdateStatusGroup.class})ListValue(vals {0, 1}, groups {AddGroup.class, UpdateStatusGroup.class})private Integer showStatus;/*** 检索首字母*/NotEmpty(groups {AddGroup.class})Pattern(regexp ^[a-zA-Z]$, message 检索首字母必须是一个字母, groups {AddGroup.class, UpdateGroup.class})private String firstLetter;/*** 排序*/NotNull(groups {AddGroup.class})Min(value 0, message 排序必须大于等于0, groups {AddGroup.class, UpdateGroup.class})private Integer sort;
}
RequestMapping(/save)
//RequiresPermissions(product:brand:save)
public R save(Validated({AddGroup.class}) RequestBody BrandEntity brand/*,BindingResult result*/) {brandService.save(brand);return R.ok();
}
JSR303 1、给Bean添加校验注解:javax.validation.constraints并定义自己的message提示 2)、开启校验功能Valid 效果校验错误以后会有默认的响应 3、给校验的参数bean后紧跟一个BindingResult就可以获取到校验的结果 4、分组校验多场景的复杂校验 1)、 NotBlank(message 品牌名必须提交,groups {AddGroup.class,UpdateGroup.class}) 给校验注解标注什么情况需要进行校验 2、Validated({AddGroup.class}) 3)、默认没有指定分组的校验注解NotBlank在分组校验情况Validated({AddGroup.class})下不生效只会在Validated生效 5、自定义校验 1、编写一个自定义的校验注解 2、编写一个自定义的校验器 ConstraintValidator 3、关联自定义的校验器和自定义的校验注解 自定义校验注解
/*** 自定义校验注解 声明可以取那些值* author xxh*/
Documented
Constraint(validatedBy {ListValueConstraintValidator.class})
Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
Retention(RUNTIME)
public interface ListValue {String message() default {com.xxh.common.valid.ListValue.message};Class?[] groups() default {};Class? extends Payload[] payload() default {};int[] values() default {};
}
自定义校验器
package com.xxh.common.valid;/*** author xxh*/
public class ListValueConstraintValidator implements ConstraintValidatorListValue, Integer {private final SetInteger set new HashSet();/*** 初始化方法* 参数自定义注解的详细信息*/Overridepublic void initialize(ListValue constraintAnnotation) {int[] values constraintAnnotation.values();for (int val : values) {set.add(val);}}/*** 判断是否校验成功** param value 需要校验的值* param context* return*/Overridepublic boolean isValid(Integer value, ConstraintValidatorContext context) {return set.contains(value);}
}
创建校验信息提示配置文件
在resource文件下创建ValidationMessages.properties
com.xxh.common.valid.ListValue.message必须提交指定的值