网站建设服务方案ppt模板,银川森林半岛,那个网站有用director做的片头,广州网站制作是什么目标
自定义一个用于校验邮政编码格式的注解ZipCode#xff0c;能够和现有的 Validation 兼容#xff0c;使用方式和其他校验注解保持一致#xff08;使用 Valid 注解接口参数#xff09;。
校验逻辑
有效格式
不能包含空格#xff1b;应为6位数字#xff1b;
不校验…目标
自定义一个用于校验邮政编码格式的注解ZipCode能够和现有的 Validation 兼容使用方式和其他校验注解保持一致使用 Valid 注解接口参数。
校验逻辑
有效格式
不能包含空格应为6位数字
不校验非空
邮政编码校验的是格式不校验是否为空null 或 空字符串。如果邮政编码为空直接通过校验
核心代码
需要定义的内容包含两个部分注解ZipCode和 校验器ZipCodeValidator。
注解ZipCode
package com.example.core.validation.zipcode;import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** 邮政编码。字符串必须是格式正确的邮政编码。正确格式为6位数字。* p* {code null} 或 空字符串是有效的能够通过校验。* p* 支持的类型字符串** author songguanxun* since 1.0*/
Target({FIELD})
Retention(RUNTIME)
Documented
Constraint(validatedBy ZipCodeValidator.class)
public interface ZipCode {/*** return the error message template*/String message() default 邮政编码格式错误;/*** return the groups the constraint belongs to*/Class?[] groups() default {};/*** return the payload associated to the constraint*/Class? extends Payload[] payload() default {};}
校验器ZipCodeValidator
package com.example.core.validation.zipcode;import com.example.core.constant.PatternConstant;
import org.springframework.util.ObjectUtils;import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;/*** 邮政编码格式校验器*/
public class ZipCodeValidator implements ConstraintValidatorZipCode, String {Overridepublic void initialize(ZipCode constraintAnnotation) {ConstraintValidator.super.initialize(constraintAnnotation);}Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {if (ObjectUtils.isEmpty(value)) {return true;}if (value.contains( )) {resetMessage(context, 邮政编码格式错误不能包含空格);return false;}if (!isValid(value)) {resetMessage(context, 邮政编码格式错误);return false;}return true;}// 格式为6位数字private static final Pattern PATTERN Pattern.compile(PatternConstant.ZIP_CODE);/*** 是有效的格式*/private boolean isValid(CharSequence input) {return PATTERN.matcher(input).matches();}/*** 重置提示信息*/private void resetMessage(ConstraintValidatorContext context, String messageTemplate) {context.disableDefaultConstraintViolation();context.buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation();}}
用到的常量
package com.example.core.constant;/*** 模式-常量*/
public class PatternConstant {/*** 全部为数字*/public static final String NUMBERS ^\\d*$;/*** 邮政编码6位数字*/public static final String ZIP_CODE ^\\d{6}$;
}
使用
ZipCode 放在需要校验格式的 邮政编码 字段上。
新增用户Param
package com.example.web.response.model.param;import com.example.core.constant.PatternConstant;
import com.example.core.validation.zipcode.ZipCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;Schema(name 新增用户Param)
public class UserAddParam {// 其他字段ZipCodeSchema(description 邮政编码, example 201100, pattern PatternConstant.ZIP_CODE)private String zipCode;}
校验效果
包含空格 不是6位数字
数字超过6位
包含非数字的字符 文章转载自: http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn