栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring MVC通过添加自定义注解格式化数据的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC通过添加自定义注解格式化数据的方法

springmvc 自定义注解 以及自定义注解的解析

一、自定义注解名字

@Target({ElementType.TYPE, ElementType.METHOD})  //类名或方法上
@Retention(RetentionPolicy.RUNTIME)//运行时

@component//自定义多个注解,且在一个类中添加两个或以上的,只需要加一个 否则会实例化多次。
public @interface SocketMapping {
 String value() default "";//参数
}

二、测试类

@SocketMapping("/a")
public class TestAnno {

 @SocketMapping(value="/b")
 public void ss(){
 System.out.println(11);
 }
 
}

三、解析测试类所在的包,反射

ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
 

  Resource[] res = rpr.getResources("classpath*:websocket
@documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface MyDateTimeFormat {

 String style() default "SS";

 ISO iso() default ISO.NONE;

 String pattern() default "";

}

@MyDateTimeFormat注解处理类

package cn.jpush.iportal.common.support;

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 extends EmbeddedValueResolutionSupport
  implements AnnotationFormatterFactory {

 private static final Set> FIELD_TYPES;

 static {
  Set> fieldTypes = new HashSet>(4);
  fieldTypes.add(Date.class);
  fieldTypes.add(Calendar.class);
  fieldTypes.add(Long.class);
  FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
 }

 @Override
 public Set> 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 getFormatter(MyDateTimeFormat annotation, Class fieldType) {
  MyDateFormatter formatter = new MyDateFormatter();
  formatter.setStylePattern(resolveEmbeddedValue(annotation.style()));
  formatter.setIso(annotation.iso());
  formatter.setPattern(resolveEmbeddedValue(annotation.pattern()));
  return formatter;
 }

}

重载parse接口

通过调用原来的处理函数super.parse(text, locale) ,得到转化的Date对象,然后再添加相关的处理业务,然后返回Date。

package cn.jpush.iportal.common.support;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.format.datetime.DateFormatter;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MyDateFormatter extends DateFormatter {

 @Override
 public Date parse(String text, Locale locale) throws ParseException {
  Date target = super.parse(text, locale);
  //+1天
  Date date = DateUtils.ceiling(new Date(target.getTime() + 1), Calendar.DATE);
  //减1ms,得出23:59:59
  Date result =new Date(date.getTime()-1);
  return result;
 }
}

向SpringMVC注册我们的自定义注解处理类

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{

 @Override
 public void addFormatters(FormatterRegistry registry) {
  MyDataTimeFormatAnnotationFormatterFactory annoFormater =new MyDataTimeFormatAnnotationFormatterFactory();
  registry.addFormatterForFieldAnnotation(annoFormater);
 }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号