Commit 2f64b9c85ef72ec7a4192908f2db274d02078967
1 parent
3435f0e7
字典拦截器性能优化(无@dict注解不走拦截器)
json格式化代码优化
Showing
1 changed file
with
43 additions
and
11 deletions
jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java
@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; | @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; | ||
4 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
5 | import com.alibaba.fastjson.parser.Feature; | 5 | import com.alibaba.fastjson.parser.Feature; |
6 | import com.baomidou.mybatisplus.core.metadata.IPage; | 6 | import com.baomidou.mybatisplus.core.metadata.IPage; |
7 | -import com.fasterxml.jackson.annotation.JsonFormat; | ||
8 | import com.fasterxml.jackson.core.JsonProcessingException; | 7 | import com.fasterxml.jackson.core.JsonProcessingException; |
9 | import com.fasterxml.jackson.databind.ObjectMapper; | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
10 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; |
@@ -25,7 +24,6 @@ import org.springframework.stereotype.Component; | @@ -25,7 +24,6 @@ import org.springframework.stereotype.Component; | ||
25 | import org.springframework.util.StringUtils; | 24 | import org.springframework.util.StringUtils; |
26 | 25 | ||
27 | import java.lang.reflect.Field; | 26 | import java.lang.reflect.Field; |
28 | -import java.text.SimpleDateFormat; | ||
29 | import java.util.*; | 27 | import java.util.*; |
30 | import java.util.concurrent.TimeUnit; | 28 | import java.util.concurrent.TimeUnit; |
31 | import java.util.stream.Collectors; | 29 | import java.util.stream.Collectors; |
@@ -46,6 +44,11 @@ public class DictAspect { | @@ -46,6 +44,11 @@ public class DictAspect { | ||
46 | @Autowired | 44 | @Autowired |
47 | public RedisTemplate redisTemplate; | 45 | public RedisTemplate redisTemplate; |
48 | 46 | ||
47 | + @Autowired | ||
48 | + private ObjectMapper objectMapper; | ||
49 | + | ||
50 | + private static final String JAVA_UTIL_DATE = "java.util.Date"; | ||
51 | + | ||
49 | /** | 52 | /** |
50 | * 定义切点Pointcut | 53 | * 定义切点Pointcut |
51 | */ | 54 | */ |
@@ -60,7 +63,7 @@ public class DictAspect { | @@ -60,7 +63,7 @@ public class DictAspect { | ||
60 | long time2=System.currentTimeMillis(); | 63 | long time2=System.currentTimeMillis(); |
61 | log.debug("获取JSON数据 耗时:"+(time2-time1)+"ms"); | 64 | log.debug("获取JSON数据 耗时:"+(time2-time1)+"ms"); |
62 | long start=System.currentTimeMillis(); | 65 | long start=System.currentTimeMillis(); |
63 | - this.parseDictText(result); | 66 | + result=this.parseDictText(result); |
64 | long end=System.currentTimeMillis(); | 67 | long end=System.currentTimeMillis(); |
65 | log.debug("注入字典到JSON数据 耗时"+(end-start)+"ms"); | 68 | log.debug("注入字典到JSON数据 耗时"+(end-start)+"ms"); |
66 | return result; | 69 | return result; |
@@ -88,7 +91,7 @@ public class DictAspect { | @@ -88,7 +91,7 @@ public class DictAspect { | ||
88 | * 目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用 | 91 | * 目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用 |
89 | * @param result | 92 | * @param result |
90 | */ | 93 | */ |
91 | - private void parseDictText(Object result) { | 94 | + private Object parseDictText(Object result) { |
92 | if (result instanceof Result) { | 95 | if (result instanceof Result) { |
93 | if (((Result) result).getResult() instanceof IPage) { | 96 | if (((Result) result).getResult() instanceof IPage) { |
94 | List<JSONObject> items = new ArrayList<>(); | 97 | List<JSONObject> items = new ArrayList<>(); |
@@ -97,13 +100,23 @@ public class DictAspect { | @@ -97,13 +100,23 @@ public class DictAspect { | ||
97 | List<Field> dictFieldList = new ArrayList<>(); | 100 | List<Field> dictFieldList = new ArrayList<>(); |
98 | // 字典数据列表, key = 字典code,value=数据列表 | 101 | // 字典数据列表, key = 字典code,value=数据列表 |
99 | Map<String, List<String>> dataListMap = new HashMap<>(5); | 102 | Map<String, List<String>> dataListMap = new HashMap<>(5); |
103 | + //取出结果集 | ||
104 | + List<Object> records=((IPage) ((Result) result).getResult()).getRecords(); | ||
105 | + //update-begin--Author:zyf -- Date:20220606 ----for:【VUEN-1230】 判断是否含有字典注解,没有注解返回----- | ||
106 | + Boolean hasDict= checkHasDict(records); | ||
107 | + if(!hasDict){ | ||
108 | + return result; | ||
109 | + } | ||
100 | 110 | ||
101 | - for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) { | ||
102 | - ObjectMapper mapper = new ObjectMapper(); | 111 | + log.info(" __ 进入字典翻译切面 DictAspect —— " ); |
112 | + //update-end--Author:zyf -- Date:20220606 ----for:【VUEN-1230】 判断是否含有字典注解,没有注解返回----- | ||
113 | + for (Object record : records) { | ||
103 | String json="{}"; | 114 | String json="{}"; |
104 | try { | 115 | try { |
116 | + //update-begin--Author:zyf -- Date:20220531 ----for:【issues/#3629】 DictAspect Jackson序列化报错----- | ||
105 | //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat | 117 | //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat |
106 | - json = mapper.writeValueAsString(record); | 118 | + json = objectMapper.writeValueAsString(record); |
119 | + //update-end--Author:zyf -- Date:20220531 ----for:【issues/#3629】 DictAspect Jackson序列化报错----- | ||
107 | } catch (JsonProcessingException e) { | 120 | } catch (JsonProcessingException e) { |
108 | log.error("json解析失败"+e.getMessage(),e); | 121 | log.error("json解析失败"+e.getMessage(),e); |
109 | } | 122 | } |
@@ -137,10 +150,12 @@ public class DictAspect { | @@ -137,10 +150,12 @@ public class DictAspect { | ||
137 | this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(","))); | 150 | this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(","))); |
138 | } | 151 | } |
139 | //date类型默认转换string格式化日期 | 152 | //date类型默认转换string格式化日期 |
140 | - if (CommonConstant.JAVA_UTIL_DATE.equals(field.getType().getName())&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){ | ||
141 | - SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
142 | - item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); | ||
143 | - } | 153 | + //update-begin--Author:zyf -- Date:20220531 ----for:【issues/#3629】 DictAspect Jackson序列化报错----- |
154 | + //if (JAVA_UTIL_DATE.equals(field.getType().getName())&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){ | ||
155 | + //SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
156 | + // item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); | ||
157 | + //} | ||
158 | + //update-end--Author:zyf -- Date:20220531 ----for:【issues/#3629】 DictAspect Jackson序列化报错----- | ||
144 | } | 159 | } |
145 | items.add(item); | 160 | items.add(item); |
146 | } | 161 | } |
@@ -186,6 +201,7 @@ public class DictAspect { | @@ -186,6 +201,7 @@ public class DictAspect { | ||
186 | } | 201 | } |
187 | 202 | ||
188 | } | 203 | } |
204 | + return result; | ||
189 | } | 205 | } |
190 | 206 | ||
191 | /** | 207 | /** |
@@ -394,4 +410,20 @@ public class DictAspect { | @@ -394,4 +410,20 @@ public class DictAspect { | ||
394 | return textValue.toString(); | 410 | return textValue.toString(); |
395 | } | 411 | } |
396 | 412 | ||
413 | + /** | ||
414 | + * 检测返回结果集中是否包含Dict注解 | ||
415 | + * @param records | ||
416 | + * @return | ||
417 | + */ | ||
418 | + private Boolean checkHasDict(List<Object> records){ | ||
419 | + if(oConvertUtils.isNotEmpty(records)){ | ||
420 | + for (Field field : oConvertUtils.getAllFields(records.get(0))) { | ||
421 | + if (oConvertUtils.isNotEmpty(field.getAnnotation(Dict.class))) { | ||
422 | + return true; | ||
423 | + } | ||
424 | + } | ||
425 | + } | ||
426 | + return false; | ||
427 | + } | ||
428 | + | ||
397 | } | 429 | } |