Skip to content

Commit 9f5d628

Browse files
🚔 Jackson按照字段顺序序列化输出
1 parent 79ff209 commit 9f5d628

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

docs/db/mysql_spring_boot_plus.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, vers
309309
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (29, 1, 4002, 1, null, 0, '2019-10-26 22:16:19', null);
310310
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (30, 1, 4003, 1, null, 0, '2019-10-26 22:16:19', null);
311311
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (31, 1, 4004, 1, null, 0, '2019-10-26 22:16:19', null);
312-
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (32, 1, 4005, 1, null, 0, '2019-10-26 22:16:19', null);
313312
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (100, 1, 1, 1, null, 0, '2019-10-26 22:16:19', null);
314313
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (101, 1, 100, 1, null, 0, '2019-10-26 22:16:19', null);
315314
INSERT INTO sys_role_permission (id, role_id, permission_id, state, remark, version, create_time, update_time) VALUES (102, 1, 1000, 1, null, 0, '2019-10-26 22:16:19', null);

src/main/java/io/geekidea/springbootplus/core/aop/AbstractLogAop.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.geekidea.springbootplus.core.aop;
1818

19-
import com.alibaba.fastjson.JSON;
2019
import com.alibaba.fastjson.JSONObject;
2120
import io.geekidea.springbootplus.common.api.ApiCode;
2221
import io.geekidea.springbootplus.common.api.ApiResult;
@@ -25,6 +24,7 @@
2524
import io.geekidea.springbootplus.util.AnsiUtil;
2625
import io.geekidea.springbootplus.util.DateUtil;
2726
import io.geekidea.springbootplus.util.IpUtil;
27+
import io.geekidea.springbootplus.util.Jackson;
2828
import lombok.Data;
2929
import lombok.extern.slf4j.Slf4j;
3030
import org.apache.commons.lang3.ArrayUtils;
@@ -294,9 +294,9 @@ protected String formatRequestInfo(Map<String, Object> map) {
294294
String requestInfo = null;
295295
try {
296296
if (logAopConfig.isRequestLogFormat()) {
297-
requestInfo = "\n" + JSON.toJSONString(map, true);
297+
requestInfo = "\n" + Jackson.toJsonString(map, true);
298298
} else {
299-
requestInfo = JSON.toJSONString(map);
299+
requestInfo = Jackson.toJsonString(map);
300300
}
301301
} catch (Exception e) {
302302
e.printStackTrace();
@@ -323,9 +323,9 @@ protected String formatResponseInfo(ApiResult apiResult) {
323323
String responseResultInfo = "responseResult:";
324324
try {
325325
if (logAopConfig.isResponseLogFormat()) {
326-
responseResultInfo += "\n" + JSON.toJSONString(apiResult, true);
326+
responseResultInfo += "\n" + Jackson.toJsonString(apiResult, true);
327327
} else {
328-
responseResultInfo += JSON.toJSONString(apiResult);
328+
responseResultInfo += Jackson.toJsonString(apiResult);
329329
}
330330
int code = apiResult.getCode();
331331
if (code == ApiCode.SUCCESS.getCode()) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.util;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.SerializationFeature;
22+
23+
/**
24+
* Jackson序列化工具类
25+
*
26+
* @author geekidea
27+
* @date 2019-11-01
28+
**/
29+
public class Jackson {
30+
31+
/**
32+
* 键按自然顺序输出
33+
*
34+
* @param object
35+
* @return
36+
*/
37+
public static String toJsonString(Object object) {
38+
return toJsonString(object, false);
39+
}
40+
41+
/**
42+
* 键按自然顺序格式化输出
43+
*
44+
* @param object
45+
* @param prettyFormat
46+
* @return
47+
*/
48+
public static String toJsonString(Object object, boolean prettyFormat) {
49+
ObjectMapper objectMapper = new ObjectMapper();
50+
try {
51+
//格式化输出
52+
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, prettyFormat);
53+
//键按自然顺序输出
54+
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
55+
return objectMapper.writeValueAsString(object);
56+
} catch (JsonProcessingException e) {
57+
e.printStackTrace();
58+
}
59+
return null;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)