用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后,开始执行业务逻辑之前,会将指定字段转化完时区并参与执行业务逻辑。