新增注解
@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切面類(lèi)
@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 {
//轉(zhuǎn)換
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++) {
//當(dāng)前只支持判斷對(duì)象類(lèi)型參數(shù)
convertObject(objects[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("參數(shù)異常");
}
}
private void convertObject(Object obj) throws IllegalAccessException {
if (Objects.isNull(obj)) {
log.info("當(dāng)前需要轉(zhuǎn)換的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) {
//獲取訪問(wèn)權(quán)
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);
}
}
}
}
}
/**
* 獲取類(lèi)的所有屬性(含所有父級(jí)類(lèi))
* @param clazz
* @param containSuperClass
* @return
*/
private List<Field> getSuperFields(Class<?> clazz, boolean containSuperClass) {
List<Field> fieldList = new ArrayList<>();
//取父類(lèi)屬性
while (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); //添加當(dāng)前類(lèi)全部屬性
if(containSuperClass){
// 父類(lèi)
clazz = clazz.getSuperclass();
}else{
clazz = null;
}
}
return fieldList;
}
}
Controller傳參字段添加注解
@Data
public class TestDto {
/**
* 查詢的開(kāi)始時(shí)間
*/
@MyDateFormatDeserializer
private String queryStartTime;
/**
* 查詢的結(jié)束時(shí)間
*/
@MyDateFormatDeserializer
private String queryEndTime;
}
結(jié)束
這樣,前端請(qǐng)求進(jìn)到Controller后,開(kāi)始執(zhí)行業(yè)務(wù)邏輯之前,會(huì)將指定字段轉(zhuǎn)化完時(shí)區(qū)并參與執(zhí)行業(yè)務(wù)邏輯。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-638011.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-638011.html
到了這里,關(guān)于用AOP實(shí)現(xiàn)前端傳參時(shí)間的時(shí)區(qū)轉(zhuǎn)化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!