Spring自定义注解(验证身份证+性别+地区)
第一步:定义注解

PersonFormId:
package com.wbg.maven1128.demo_formatter; import java.lang.annotation.*;
@Documented
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonFormId {
}
第二步:创建实体类

Person:
package com.wbg.maven1128.demo_formatter;
import java.util.Date;
public class Person {
Date birthday;
String sex;
String province;
public Person() {
}
@Override
public String toString() {
return "Person{" +
"birthday='" + birthday + '\'' +
", sex='" + sex + '\'' +
", province='" + province + '\'' +
'}';
}
public Person(Date birthday, String sex, String province) {
this.birthday = birthday;
this.sex = sex;
this.province = province;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
第三步:创建实现类调用接口Formatter<Person>

PersonFormatter:
package com.wbg.maven1128.demo_formatter; import org.springframework.format.Formatter;
import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map; public class PersonFormatter implements Formatter<Person> { @Override
public String print(Person object, Locale locale) {
return null;
}
@Override
public Person parse(String text, Locale locale) throws ParseException {
if(text.length()!=18){
throw new ParseException("请输入18位身份证",59);
}
else if(text.length()==18&&Verification(text)){ return getPerson(text);
}else {
throw new ParseException("身份证输入错误",59);
}
}
Person getPerson(String text){
Person person=new Person();
Calendar instance = Calendar.getInstance();
int year=Integer.parseInt(text.substring(6,10));
int month=Integer.parseInt(text.substring(10,12))-1;
int date=Integer.parseInt(text.substring(12,14));
instance.set(year,month,date);
person.setBirthday(instance.getTime());
person.setProvince(provinces.get(Integer.parseInt(text.substring(0,2))));
person.setSex(Integer.parseInt(text.substring(16,17))%2==0?"女":"男");
return person;
}
static Map<Integer,String> provinces = new HashMap<Integer, String>(){{
this.put(11,"北京市");
this.put(12,"天津市");
this.put(13,"河北省");
this.put(14,"山西省");
this.put(15,"内蒙古自治区");
this.put(21,"辽宁省");
this.put(22,"吉林省");
this.put(23,"黑龙江省");
this.put(31,"上海市");
this.put(32,"江苏省");
this.put(33,"浙江省");
this.put(34,"安徽省");
this.put(35,"福建省");
this.put(36,"江西省");
this.put(37,"山东省");
this.put(41,"河南省");
this.put(42,"湖北省");
this.put(43,"湖南省");
this.put(44,"广东省");
this.put(45,"广西壮族自治区");
this.put(46,"海南省");
this.put(51,"四川省");
this.put(52,"贵州省");
this.put(53,"云南省");
this.put(54,"西藏自治区");
this.put(50,"重庆市");
this.put(61,"陕西省");
this.put(62,"甘肃省");
this.put(63,"青海省");
this.put(64,"宁夏回族自治区");
this.put(65,"新疆维吾尔自治区");
this.put(83,"台湾地区");
this.put(81,"香港特别行政区");
this.put(82,"澳门特别行政区");
}}; boolean Verification(String text){
text=text.toUpperCase();
String []aa={"7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"};
String []bb={"1","0","X","9","8","7","6","5","4","3","2"};
int sum=0;
for (int i = 0; i < 17; i++) {
sum+=Integer.parseInt(text.substring(i, 1 + i))*Integer.parseInt(aa[i]);
}
return bb[sum%11].equals(text.substring(17,18));
} }
第四步:调用

MyDataTimeFormatAnnotationFormatterFactory接口AnnotationFormatterFactory<MyDateFormatter>
package com.wbg.maven1128.intface; import com.wbg.maven1128.entity.Person;
import org.springframework.context.support.EmbeddedValueResolutionSupport;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Formatter;
import org.springframework.format.Parser;
import org.springframework.format.Printer; import java.util.*; public class MyDataTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<MyDateTimeFormat> {
private static final Set<Class<?>> FIELD_TYPES;
static {
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
//添加可以使用注解的类型
fieldTypes.add(String.class);
fieldTypes.add(Person.class);
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
}
@Override
public Set<Class<?>> getFieldTypes() {
return FIELD_TYPES;
} @Override
public Printer<?> getPrinter(MyDateTimeFormat annotation, Class<?> fieldType) {
return getFormatter(annotation, fieldType);
} @Override
public Parser<?> getParser(MyDateTimeFormat annotation, Class<?> fieldType) {
return getFormatter(annotation, fieldType);
}
protected Formatter<Person> getFormatter(MyDateTimeFormat annotation, Class<?> fieldType) {
MyDateFormatter formatter = new MyDateFormatter();
return formatter;
}
}
第四步:启用注解

<mvc:default-servlet-handler />
<bean name="factoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.wbg.maven1128.demo_formatter.PersonAnnotationFormatter" >
</bean>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="factoryBean"/>
jsp页面:

controller

package com.wbg.maven1128.controller; import com.wbg.maven1128.demo_formatter.Person;
import com.wbg.maven1128.demo_formatter.PersonFormId;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/person")
public class PersonController {
@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
@ResponseBody
public String getPerson(@RequestParam(value = "pid",required = false)@PersonFormId Person person){
try {
return person.toString();
}catch (Exception e){
System.out.println(e.getMessage());
return e.getMessage();
}
}
@GetMapping
public String index(){
return "person_index";
}
}
最后直接测试


Spring自定义注解(验证身份证+性别+地区)的更多相关文章
- spring自定义注解实现登陆拦截器
1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...
- SpringMVC拦截器+Spring自定义注解实现权限验证
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- spring - 自定义注解
本自定义注解的作用:用于控制类方法的调用,只有拥有某个角色时才能调用. java内置注解 1.@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: ElemenetTy ...
- Spring 自定义注解,配置简单日志注解
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- 照虎画猫写自己的Spring——自定义注解
Fairy已经实现的功能 读取XML格式配置文件,解析得到Bean 读取JSON格式配置文件,解析得到Bean 基于XML配置的依赖注入 所以,理所当然,今天该实现基于注解的依赖注入了. 基于XML配 ...
- 使用Spring自定义注解实现任务路由的方法
在Spring mvc的开发中,我们可以通过RequestMapping来配,当前方法用于处理哪一个URL的请求.同样我们现在有一个需求,有一个任务调度器,可以按照不同的任务类型路由到不同的任务执行器 ...
- Spring自定义注解扫描的实现
目标:实现自定义spring自动扫描注解.主要为后期实现分布式服务框架自动注解提供技术支持 技术分析:通过配置组件扫描标签使spring解析标签. 1. JewelScanBeanDefaultPar ...
- 2018-02-11 发布 spring 自定义注解(annotation)与 aop获取注解
知识点: Java自定义注解.spring aop @aspect的使用 首先我们先介绍Java自定义注解. 在开发过程中,我们实现接口的时候,会出现@Override,有时还会提示写@Suppres ...
- 深入Spring:自定义注解加载和使用
前言 在工作中经常使用Spring的相关框架,免不了去看一下Spring的实现方法,了解一下Spring内部的处理逻辑.特别是开发Web应用时,我们会频繁的定义@Controller,@Service ...
随机推荐
- java 的底层通信--Socket
以前一直不太重视java 基础的整理,感觉在实际开发中好像java 基础用处不大,感觉不理解一些底层的东西对开发工作影响也不大.不过,后来我发现,很多东西都是相互联系的,如果底层的东西你不理解,后面的 ...
- poj 3260 最少硬币(01+多重+完全背包)
http://www.cnblogs.com/ACMan/archive/2012/08/14/2637437.html #include <iostream> #include < ...
- js-原始类型和声明变量
** Java的基本数据类型:byte.short.int.long.float.double.char.boolean ** 定义变量 都是用关键字 var(ES6中可以使用const和let来定义 ...
- javaScript删除对象、数组中的null、undefined、空对象、空数组方法
这两天在项目中遇到后台需要传的数据为不能有null,不能有空值,而这个数据又是一个庞大的对象,对组集合,所以写了个方法来解决这个问题.为了兼具所有的种类类型,封装了方法,代码如下: let obj = ...
- 软件项目技术点(9)——如何将gif动态图拆分绘制
AxeSlide软件项目梳理 canvas绘图系列知识点整理 背景介绍 我们的软件支持插入gif图片,并且展示在软件里是动态的,例如插入下面这张gif图. 在软件里显示的同样是这样的动态效果: 那 ...
- XPath路径表达式笔记(转载)
简单说,xpath就是选择XML文件中节点的方法. 所谓节点(node),就是XML文件的最小构成单位,一共分成7种. - element(元素节点)- attribute(属性节点)- text ( ...
- Android解析ActivityManagerService(二)ActivityTask和Activity栈管理
前言 关于AMS,原计划是只写一篇文章来介绍,但是AMS功能繁多,一篇文章的篇幅远远不够.这一篇我们接着来学习与AMS相关的ActivityTask和Activity栈管理. 1.ActivitySt ...
- Python学习系列----第四章 函数
4.1 函数定义 函数是python中重要的工具.函数用关键字 def 来定义.def 关键字后跟一个函数的标识符名称,然后跟一对圆括号.圆括号之中可以包括一些变量名,该行以冒号结尾.接下来是一块 ...
- MySQL案例之Timestamp和Datetime
mysql数据库常用的时间类型有timestamp和datetime,两者主要区别是占用存储空间长度不一致.可存储的时间也有限制,但针对不同版本下,timestamp字段类型的设置需要慎重,因为不注意 ...
- .net 面向对象程序设计深入](3)UML
1.活动图简介 定义:是阐明了业务用例实现的工作流程. 业务工作流程说明了业务为向所服务的业务主角提供其所需的价值而必须完成的工作. 业务用例由一系列活动组成,它们共同为业务主角生成某些工件. 工作流 ...