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

用AOP实现前端传参时间的时区转化

用AOP实现前端传参时间的时区转化

  • 新增注解
  • 新增AOP切面类
  • Controller传参字段添加注解
  • 结束

新增注解

@Documented
@Target({FIELD,METHOD,PARAMETER,ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface MyDateFormatDeserializer {String pattern() default "yyyy-MM-dd HH:mm:ss";String oldPattern() default "yyyy-MM-dd HH:mm:ss";
}

新增AOP切面类

@Aspect
@Component
public class MyDateFormatDeserializerAspect {private static Logger log = LoggerFactory.getLogger(MyDateFormatDeserializerAspect.class);@Pointcut("execution(* com.jovision.vse.*.*.web.controller..*.*(..))")public void pointCut() {}@Around("pointCut()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {//转换dateFormat(joinPoint);return joinPoint.proceed();}public void dateFormat(ProceedingJoinPoint joinPoint) {Object[] objects = null;try {objects = joinPoint.getArgs();if (objects.length != 0) {for (int i = 0; i < objects.length; i++) {//当前只支持判断对象类型参数convertObject(objects[i]);}}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("参数异常");}}private void convertObject(Object obj) throws IllegalAccessException {if (Objects.isNull(obj)) {log.info("当前需要转换的object为null");return;}String timeZoneStr = CurrentUserUtil.currentTimeZone();if(StringUtils.isNotBlank(timeZoneStr)){List<Field> fieldList = getSuperFields(obj.getClass(),true);for (Field field : fieldList) {boolean containFormatField = field.isAnnotationPresent(MyDateFormatDeserializer.class);if (containFormatField) {//获取访问权field.setAccessible(true);MyDateFormatDeserializer annotation = field.getAnnotation(MyDateFormatDeserializer.class);String oldPattern = annotation.oldPattern();String newPattern = annotation.pattern();Object dateValue = field.get(obj);if (Objects.nonNull(dateValue) && !StringUtils.isEmpty(oldPattern) && !StringUtils.isEmpty(newPattern)) {String newValue = StringUtils.EMPTY;try {Date date = new Date();if(dateValue instanceof Date){date = (Date) dateValue;}else if(dateValue instanceof String){date = DateUtils.parseDate(dateValue.toString(), oldPattern);}else{log.error("parse date error,@MyDateFormatDeserializer must String or Date !!!");return;}SimpleDateFormat currentTime = new SimpleDateFormat(newPattern);TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);currentTime.setTimeZone(timeZone);newValue = currentTime.format(date);} catch (ParseException e) {throw new RuntimeException(e);}log.info("aop transform success - oldValue = {}, newValue = {} ",dateValue , newValue);field.set(obj, newValue);}}}}}/*** 获取类的所有属性(含所有父级类)* @param clazz* @param containSuperClass* @return*/private List<Field> getSuperFields(Class<?> clazz, boolean containSuperClass) {List<Field> fieldList = new ArrayList<>();//取父类属性while (clazz != null) {fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); //添加当前类全部属性if(containSuperClass){// 父类clazz = clazz.getSuperclass();}else{clazz = null;}}return fieldList;}
}

Controller传参字段添加注解

@Data
public class TestDto {/*** 查询的开始时间*/@MyDateFormatDeserializerprivate String queryStartTime;/*** 查询的结束时间*/@MyDateFormatDeserializerprivate String queryEndTime;
}

结束

这样,前端请求进到Controller后,开始执行业务逻辑之前,会将指定字段转化完时区并参与执行业务逻辑。

http://www.lryc.cn/news/117459.html

相关文章:

  • mybatis There is no getter for property named ‘*‘ in ‘class java.lang.String
  • Mac终端前总会出现 (base) 字样解决
  • RabbitMQ面试题大全含答案
  • Linux配置QT Creator环境:ubuntu中安装QT Creator环境
  • 机器学习深度学习——池化层
  • siMLPe:Human Motion Prediction
  • 详解——JS map()方法
  • leetcode做题笔记57
  • SAP Fiori 将GUI中的自开发报表添加到Fiori 工作台
  • 【Docker】配置指定大小的磁盘空间
  • 使用Spring五大注解来更加简单的存储Bean对象
  • Netty面试题1
  • 水质分析积分球定义和原理
  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[基础知识]
  • phpstorm添加vue 标签属性绑定提示和提示vue的方法提示
  • 从计算到人类知识:ChatGPT与智能演化
  • Leetcode每日一题:2681. 英雄的力量(2023.8.1 C++)
  • 【学习】若依源码(前后端分离版)之 “ 异常处理”
  • 天花板级,Python接口自动化测试-接口关联封装调用(实例)
  • yolov5代码解读之yolo.py【网络结构】
  • Docker之jenkins部署harbor在harbor中完成部署
  • 安装Jenkins
  • 大运空瓶行动,绘就生态文明画卷
  • tomcat7.exe 启动闪退解决
  • java修改jar包中的配置文件
  • 半导体器件||的学习
  • jenkins流水线
  • 视频监控汇聚EasyCVR平台WebRTC流地址无法播放的原因排查
  • NOSQL——redis的安装,配置与简单操作
  • 《合成孔径雷达成像算法与实现》Figure3.7