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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
第四步:启用注解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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"; } }
最后直接测试