当前位置: 首页 > wzjs >正文

网站建设方案 doc阿里云主机怎么做两个网站吗

网站建设方案 doc,阿里云主机怎么做两个网站吗,深圳市深圳市住房和建设局网站,余姚做轴承网站目标 自定义一个用于校验邮政编码格式的注解ZipCode,能够和现有的 Validation 兼容,使用方式和其他校验注解保持一致(使用 Valid 注解接口参数)。 校验逻辑 有效格式 不能包含空格;应为6位数字; 不校验…

目标

自定义一个用于校验邮政编码格式的注解@ZipCode,能够和现有的 Validation 兼容,使用方式和其他校验注解保持一致(使用 @Valid 注解接口参数)。

校验逻辑

有效格式

  1. 不能包含空格;
  2. 应为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 ConstraintValidator<ZipCode, 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 {// 其他字段@ZipCode@Schema(description = "邮政编码", example = "201100", pattern = PatternConstant.ZIP_CODE)private String zipCode;}

校验效果

包含空格

在这里插入图片描述

不是6位数字

数字超过6位
在这里插入图片描述

包含非数字的字符
在这里插入图片描述

http://www.dtcms.com/wzjs/797916.html

相关文章:

  • 怎么建立网站?购买模板建站
  • 厦门模板做网站品牌网站设计制作哪家好
  • 能进封禁网站的手机浏览器扁平化网页设计图片
  • 广西城乡和建设厅网站首页兰州网站建设小程序
  • 花生壳做网站昆山网站建设ikelv
  • 智慧农业网站建设网站开发及代运营
  • 吉林网站制作现在个人做网站或者app还有收益
  • 网站会员和discuz会员同步wordpress更改目录插件
  • 做网站有哪些公司好网站建设素材网
  • 南昌专门做网站的人网站做填充
  • 房山网站建设公司公司做网站的费用怎么做账
  • 网站建设需求计划书环保设备网站怎么做
  • 做养殖推广什么网站好建个平台需要多少资金
  • 怎么申请自己的网站网址网站默认样式
  • 网店网站设计一个完整网站开发需要什么技术
  • 给朋友网站做宣传怎么写磐石市住房和城乡建设局网站
  • wordpress企业仿站莱芜金点子官网
  • 怎么做一个网站页面店铺装修设计网
  • xyz溢价域名最好的网站南宁建设银行官网招聘网站
  • 视频网站开发视频科技有限公司网页设计
  • 买源码做网站简单嘛wordpress 使用技巧
  • 网站建设公司介绍wordpress在线咨询
  • 做网站大概网络营销推广方式有几种
  • 网站开发建议带有响应式的网站
  • 从零开始学习网站开发计划wordpress 备案号插件
  • 不会建网站如何搭建一个服务平台
  • 办网站如何备案石家庄手机网站
  • 南山商城网站建设赣州百姓网免费发布信息网
  • 简述电子商务网站的建设步骤有什么免费做h5的素材网站
  • 性能网站建设iis打开网站变成下载