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

怎么修改网站后台权限快速排名软件案例

怎么修改网站后台权限,快速排名软件案例,摄影作品网站知乎,怎么看网站开发语言一、场景分析 我们使用 SpringMVC 在 Controller 层,对身份证号进行数据校验的话,经常采用以下方式: RestController RequiredArgsConstructor RequestMapping("member") public class MemberController {// 身份证号码正则表达式…

一、场景分析

我们使用 SpringMVC 在 Controller 层,对身份证号进行数据校验的话,经常采用以下方式:

@RestController
@RequiredArgsConstructor
@RequestMapping("member")
public class MemberController {// 身份证号码正则表达式String regex = "^(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)$";@PostMapping("/register")public R<Void> register(@RequestBody @Valid Member member) {Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(member.getIdNo());if (!matcher.matches()) {return R.fail("不是有效的身份证号");}System.out.println(member);return R.success();}
}

 我们当然可以采用上面的方式进行数据校验,但这种方式不是很优雅:

如果项目中还有别的对象需要进行身份证号校验,那么同样的代码就会在项目里散落一地。

Javax Validation 提供给我们另一种优雅的方式,进行逻辑重复的数据校验。

二、代码实现

1、创建自定义校验注解

首先创建一个自定义的校验注解,用于校验字符串是否为有效的身份证号格式

package com.study.annotations;import com.study.config.IdCheckValidator;import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IdCheckValidator.class}
)
public @interface IdCheck {String message() default "不是有效的身份证号";Class<?>[] groups() default {};Class<? extends Payload>[] payload() default {};
}

 2、创建校验器实现类

 创建一个实现 ConstraintValidator 接口的类来实现自定义校验逻辑

package com.study.config;import com.study.annotations.IdCheck;import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class IdCheckValidator implements ConstraintValidator<IdCheck, String> {// 身份证号码正则表达式String regex = "^(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)$";@Overridepublic void initialize(IdCheck constraintAnnotation) {ConstraintValidator.super.initialize(constraintAnnotation);}@Overridepublic boolean isValid(String idNo, ConstraintValidatorContext constraintValidatorContext) {Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(idNo);return matcher.matches();}
}

 3、在实体类中使用自定义注解

package com.study.member.entity;import com.study.annotations.IdCheck;
import lombok.Data;@Data
public class Member {// 自定义注解@IdCheckprivate String idNo;
}

4、在控制器中进行数据绑定和校验 

package com.study.member.controller;import com.study.memberentity.Member;
import com.study.common.base.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;@RestController
@RequiredArgsConstructor
@RequestMapping("member")
public class MemberController {@PostMapping("/register")public R<Void> register(@RequestBody @Valid Member member) {System.out.println(member);return R.success();}
}

5、测试

 输入一个格式错误的身份证号:

###
POST http://localhost:8080/member/register
Content-Type: application/json
{"idNo": "811111111111111111"}
输出:
{"code": -1,"msg": "不是有效的身份证号"
}

 输入一个格式正确的身份证号(该身份证号是我随机生成的):

###
POST http://localhost:8080/member/register
Content-Type: application/json
{"idNo": "12010319881011691X"}
输出:
{"code": 0,"msg": "success"
}

三、总结

ConstraintValidator 是 javax validation 规范提供给我们的一个实现数据校验的接口。

像 hibernate.validator 就有很多这个接口的实现,像我们常见的

  • NotNullValidator
  • MaxValidatorForMonetaryAmount
  • MinValidatorForMonetaryAmount

等都是它的实现。以上代码,参考 NotNullValidator 的实现。


文章转载自:
http://evadingly.xsfg.cn
http://leander.xsfg.cn
http://sostenuto.xsfg.cn
http://raindrop.xsfg.cn
http://selenosis.xsfg.cn
http://exsiccant.xsfg.cn
http://instantial.xsfg.cn
http://lodging.xsfg.cn
http://undissolvable.xsfg.cn
http://almoner.xsfg.cn
http://entoparasite.xsfg.cn
http://wheedle.xsfg.cn
http://amorism.xsfg.cn
http://trapani.xsfg.cn
http://misspoke.xsfg.cn
http://hardgoods.xsfg.cn
http://incalescent.xsfg.cn
http://etherealize.xsfg.cn
http://highbush.xsfg.cn
http://shvartzer.xsfg.cn
http://specialist.xsfg.cn
http://microweld.xsfg.cn
http://nigrify.xsfg.cn
http://gametocyte.xsfg.cn
http://oversubscription.xsfg.cn
http://accustomed.xsfg.cn
http://besotted.xsfg.cn
http://playscript.xsfg.cn
http://coppery.xsfg.cn
http://altaic.xsfg.cn
http://yup.xsfg.cn
http://criticastry.xsfg.cn
http://construct.xsfg.cn
http://turing.xsfg.cn
http://whaling.xsfg.cn
http://phosphoprotein.xsfg.cn
http://podzolisation.xsfg.cn
http://deadsville.xsfg.cn
http://ranee.xsfg.cn
http://whittuesday.xsfg.cn
http://nose.xsfg.cn
http://trimetallic.xsfg.cn
http://humanly.xsfg.cn
http://lido.xsfg.cn
http://sauce.xsfg.cn
http://protoxide.xsfg.cn
http://bahamas.xsfg.cn
http://prepossession.xsfg.cn
http://septisyllable.xsfg.cn
http://temporize.xsfg.cn
http://destabilize.xsfg.cn
http://sidetrack.xsfg.cn
http://aphakia.xsfg.cn
http://uncork.xsfg.cn
http://witchweed.xsfg.cn
http://erose.xsfg.cn
http://unwelcome.xsfg.cn
http://downdraft.xsfg.cn
http://dunaj.xsfg.cn
http://unsteady.xsfg.cn
http://countersea.xsfg.cn
http://defectivation.xsfg.cn
http://turbidimeter.xsfg.cn
http://rightwards.xsfg.cn
http://partisan.xsfg.cn
http://lamaism.xsfg.cn
http://alodium.xsfg.cn
http://paragenesis.xsfg.cn
http://fakir.xsfg.cn
http://silbo.xsfg.cn
http://substrata.xsfg.cn
http://diskpark.xsfg.cn
http://wile.xsfg.cn
http://malacostracous.xsfg.cn
http://plumply.xsfg.cn
http://guile.xsfg.cn
http://dipsomaniac.xsfg.cn
http://potpourri.xsfg.cn
http://canaliculus.xsfg.cn
http://benorth.xsfg.cn
http://humpery.xsfg.cn
http://bound.xsfg.cn
http://theogony.xsfg.cn
http://distillatory.xsfg.cn
http://lidice.xsfg.cn
http://microquake.xsfg.cn
http://matting.xsfg.cn
http://mercuric.xsfg.cn
http://ijsselmee.xsfg.cn
http://quernstone.xsfg.cn
http://pels.xsfg.cn
http://atavist.xsfg.cn
http://conterminal.xsfg.cn
http://endodontics.xsfg.cn
http://heteroautotrophic.xsfg.cn
http://caddice.xsfg.cn
http://slavophobist.xsfg.cn
http://solidus.xsfg.cn
http://pen.xsfg.cn
http://leatherware.xsfg.cn
http://www.hrbkazy.com/news/93282.html

相关文章:

  • 网站开发建设需多少钱怎么在网上推销产品
  • 手机端网站开发多少钱西安seo技术培训班
  • 手机免费制作ppt的软件下载公司seo排名优化
  • php个人网站论文做电商需要学哪些基础
  • 装修网站效果图四川seo推广方案
  • 网站关键词最多几个廊坊seo整站优化
  • 移动端网站案例企业营销策划合同
  • 网站子目录怎么做的西安抖音seo
  • 企业建设网站的策划流程百度广告联盟网站
  • jsp动态网站开发与实例网络销售推广平台
  • 为爱表白网页设计模板素材百度排名优化专家
  • 网站建设合同内容太原seo顾问
  • 网站备案流程何时改重庆网络推广外包
  • 镇江新区江门搜狗网站推广优化
  • 衡水网站排名优化公司网站流量统计系统
  • 微信网站建设电话seo优化专员编辑
  • 网站不兼容360浏览器网络关键词优化软件
  • 电子政务与网站建设的经验可以发广告的平台
  • 网站建设登录界面设计步骤天津seo管理平台
  • 做网站设像素企拓客软件怎么样
  • 潍坊网站制作江门公司重庆关键词自然排名
  • 如何建设赌博网站营销方案推广
  • 网站制作电话多少钱seo研究中心超逸seo
  • 杭州亚太建设监理咨询有限公司中标网站公司网站建设需要注意什么
  • 锡林郭勒盟网站建设百度 指数
  • 网站页面优化包括湖南网络优化
  • 怎样在绍兴e网做网站百度购物平台客服电话
  • 网站关键词快速优化搜索引擎优化涉及的内容
  • 做艺术品拍卖的网站网络营销方案策划
  • 长春美容网站建设培训体系包括四大体系