@rdmind/rdmind 0.1.8 → 0.1.9-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +284 -77
- package/locales/en.js +3 -1
- package/locales/zh.js +3 -0
- package/package.json +2 -2
- package/template/pom.xml +40 -0
- package/template/sns-demo-app/pom.xml +0 -5
- package/template/sns-demo-common/pom.xml +4 -5
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/constants/AppPlatform.java +57 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/enums/ErrorCodeEnum.java +30 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/exception/AppBizException.java +51 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/AssertUtils.java +137 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/JsonHelper.java +129 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/WrappedContextHelper.java +182 -0
- package/template/sns-demo-domain/pom.xml +0 -5
- package/template/sns-demo-infrastructure/pom.xml +0 -5
- package/template/sns-demo-start/pom.xml +0 -5
- package/template/sns-demo-start/src/main/resources/logback-spring.xml +16 -19
- package/templates/idl-template/wiki/.idea/codeStyles/Project.xml +7 -0
- package/templates/idl-template/wiki/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/templates/idl-template/wiki/.idea/inspectionProfiles/Project_Default.xml +5 -0
- package/templates/idl-template/wiki/.idea/misc.xml +14 -0
- package/templates/idl-template/wiki/.idea/modules.xml +8 -0
- package/templates/idl-template/wiki/.idea/vcs.xml +6 -0
- package/templates/idl-template/wiki/.idea/wiki.iml +9 -0
- package/templates/idl-template/wiki/SDK-Dev-Guide.md +0 -2
- package/templates/idl-template/wiki/example/.gitlab-ci.yml +3 -17
- package/templates/idl-template/wiki/example/base.thrift +94 -0
- package/templates/idl-template/wiki/example/common.thrift +39 -0
- package/templates/idl-template/wiki/example/dto.thrift +3 -0
- package/templates/idl-template/wiki/example/enum.thrift +1 -0
- package/templates/idl-template/wiki/example/gen-java.sh +5 -2
- package/templates/idl-template/wiki/example/maven_project/pom.xml +102 -83
- package/templates/idl-template/wiki/example/req.thrift +4 -0
- package/templates/idl-template/wiki/example/res.thrift +5 -0
- package/templates/idl-template/wiki/example/sdk-spec.yml +4 -3
- package/templates/idl-template/wiki/example/service.thrift +15 -0
- package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/enums/.gitkeep +0 -0
- package/templates/idl-template/wiki/.arcconfig +0 -3
- package/templates/idl-template/wiki/example/.arcconfig +0 -3
- package/templates/idl-template/wiki/example/hello.thrift +0 -29
- package/templates/idl-template/wiki/example/maven_project/src/main/java/com/xiaohongshu/sns/thrift/hello/HelloServiceAutoConfiguration.java +0 -46
- package/templates/idl-template/wiki/example/maven_project/src/main/java/com/xiaohongshu/sns/thrift/hello/HelloServiceProperties.java +0 -18
- package/templates/idl-template/wiki/example/maven_project/src/main/resources/META-INF/spring.factories +0 -3
- package/templates/idl-template/wiki/example/maven_project/src/test/java/com/xiaohongshu/sns/thrift/test/AutoConfigureTest.java +0 -53
package/locales/en.js
CHANGED
|
@@ -89,6 +89,9 @@ export default {
|
|
|
89
89
|
'No tools available': 'No tools available',
|
|
90
90
|
'View or change the approval mode for tool usage':
|
|
91
91
|
'View or change the approval mode for tool usage',
|
|
92
|
+
'Invalid approval mode "{{arg}}". Valid modes: {{modes}}':
|
|
93
|
+
'Invalid approval mode "{{arg}}". Valid modes: {{modes}}',
|
|
94
|
+
'Approval mode set to "{{mode}}"': 'Approval mode set to "{{mode}}"',
|
|
92
95
|
'View or change the language setting': 'View or change the language setting',
|
|
93
96
|
'change the theme': 'change the theme',
|
|
94
97
|
'Select Theme': 'Select Theme',
|
|
@@ -1068,7 +1071,6 @@ export default {
|
|
|
1068
1071
|
'Applying percussive maintenance...',
|
|
1069
1072
|
'Searching for the correct USB orientation...',
|
|
1070
1073
|
'Ensuring the magic smoke stays inside the wires...',
|
|
1071
|
-
'Rewriting in Rust for no particular reason...',
|
|
1072
1074
|
'Trying to exit Vim...',
|
|
1073
1075
|
'Spinning up the hamster wheel...',
|
|
1074
1076
|
"That's not a bug, it's an undocumented feature...",
|
package/locales/zh.js
CHANGED
|
@@ -88,6 +88,9 @@ export default {
|
|
|
88
88
|
'No tools available': '没有可用工具',
|
|
89
89
|
'View or change the approval mode for tool usage':
|
|
90
90
|
'查看或更改工具使用的审批模式',
|
|
91
|
+
'Invalid approval mode "{{arg}}". Valid modes: {{modes}}':
|
|
92
|
+
'无效的审批模式 "{{arg}}"。有效模式:{{modes}}',
|
|
93
|
+
'Approval mode set to "{{mode}}"': '审批模式已设置为 "{{mode}}"',
|
|
91
94
|
'View or change the language setting': '查看或更改语言设置',
|
|
92
95
|
'change the theme': '更改主题',
|
|
93
96
|
'Select Theme': '选择主题',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rdmind/rdmind",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9-alpha.0",
|
|
4
4
|
"description": "RDMind - AI-powered coding assistant",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "cli.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"locales"
|
|
21
21
|
],
|
|
22
22
|
"config": {
|
|
23
|
-
"sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.1.
|
|
23
|
+
"sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.1.9-alpha.0"
|
|
24
24
|
},
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|
package/template/pom.xml
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
<properties>
|
|
14
14
|
<java.version>11</java.version>
|
|
15
15
|
<root-pom.version>3.3.0-MONTHLY-SNAPSHOT</root-pom.version>
|
|
16
|
+
<lombok.version>1.18.38</lombok.version>
|
|
17
|
+
<mapstruct.version>1.6.3</mapstruct.version>
|
|
18
|
+
<jackson.version>2.12.3</jackson.version>
|
|
16
19
|
</properties>
|
|
17
20
|
|
|
18
21
|
<modules>
|
|
@@ -74,9 +77,46 @@
|
|
|
74
77
|
<artifactId>apollo-client-helper</artifactId>
|
|
75
78
|
<version>1.0.6</version>
|
|
76
79
|
</dependency>
|
|
80
|
+
<dependency>
|
|
81
|
+
<groupId>org.mapstruct</groupId>
|
|
82
|
+
<artifactId>mapstruct</artifactId>
|
|
83
|
+
<version>${mapstruct.version}</version>
|
|
84
|
+
</dependency>
|
|
85
|
+
<dependency>
|
|
86
|
+
<groupId>org.mapstruct</groupId>
|
|
87
|
+
<artifactId>mapstruct-processor</artifactId>
|
|
88
|
+
<version>${mapstruct.version}</version>
|
|
89
|
+
</dependency>
|
|
90
|
+
<dependency>
|
|
91
|
+
<groupId>com.fasterxml.jackson.core</groupId>
|
|
92
|
+
<artifactId>jackson-databind</artifactId>
|
|
93
|
+
<version>${jackson.version}</version>
|
|
94
|
+
</dependency>
|
|
95
|
+
<dependency>
|
|
96
|
+
<groupId>com.fasterxml.jackson.core</groupId>
|
|
97
|
+
<artifactId>jackson-core</artifactId>
|
|
98
|
+
<version>${jackson.version}</version>
|
|
99
|
+
</dependency>
|
|
100
|
+
<dependency>
|
|
101
|
+
<groupId>com.fasterxml.jackson.core</groupId>
|
|
102
|
+
<artifactId>jackson-annotations</artifactId>
|
|
103
|
+
<version>${jackson.version}</version>
|
|
104
|
+
</dependency>
|
|
77
105
|
</dependencies>
|
|
78
106
|
</dependencyManagement>
|
|
79
107
|
|
|
108
|
+
<dependencies>
|
|
109
|
+
<dependency>
|
|
110
|
+
<groupId>org.springframework.boot</groupId>
|
|
111
|
+
<artifactId>spring-boot-starter-actuator</artifactId>
|
|
112
|
+
</dependency>
|
|
113
|
+
<dependency>
|
|
114
|
+
<groupId>org.springframework.boot</groupId>
|
|
115
|
+
<artifactId>spring-boot-starter-test</artifactId>
|
|
116
|
+
<scope>test</scope>
|
|
117
|
+
</dependency>
|
|
118
|
+
</dependencies>
|
|
119
|
+
|
|
80
120
|
<build>
|
|
81
121
|
<plugins>
|
|
82
122
|
<plugin>
|
|
@@ -22,11 +22,6 @@
|
|
|
22
22
|
<groupId>com.xiaohongshu.sns</groupId>
|
|
23
23
|
<artifactId>sns-demo-infrastructure</artifactId>
|
|
24
24
|
</dependency>
|
|
25
|
-
<dependency>
|
|
26
|
-
<groupId>org.springframework.boot</groupId>
|
|
27
|
-
<artifactId>spring-boot-starter-test</artifactId>
|
|
28
|
-
<scope>test</scope>
|
|
29
|
-
</dependency>
|
|
30
25
|
</dependencies>
|
|
31
26
|
|
|
32
27
|
</project>
|
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
<groupId>com.xiaohongshu</groupId>
|
|
27
27
|
<artifactId>apollo-client-helper</artifactId>
|
|
28
28
|
</dependency>
|
|
29
|
+
<dependency>
|
|
30
|
+
<groupId>com.fasterxml.jackson.core</groupId>
|
|
31
|
+
<artifactId>jackson-databind</artifactId>
|
|
32
|
+
</dependency>
|
|
29
33
|
<dependency>
|
|
30
34
|
<groupId>org.projectlombok</groupId>
|
|
31
35
|
<artifactId>lombok</artifactId>
|
|
@@ -34,11 +38,6 @@
|
|
|
34
38
|
<groupId>com.xiaohongshu</groupId>
|
|
35
39
|
<artifactId>events-client</artifactId>
|
|
36
40
|
</dependency>
|
|
37
|
-
<dependency>
|
|
38
|
-
<groupId>org.springframework.boot</groupId>
|
|
39
|
-
<artifactId>spring-boot-starter-test</artifactId>
|
|
40
|
-
<scope>test</scope>
|
|
41
|
-
</dependency>
|
|
42
41
|
</dependencies>
|
|
43
42
|
|
|
44
43
|
</project>
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
package com.xiaohongshu.sns.demo.common.constants;
|
|
2
|
+
|
|
3
|
+
import java.util.HashMap;
|
|
4
|
+
import java.util.Map;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 客户端平台枚举
|
|
8
|
+
*
|
|
9
|
+
* @author RDMind
|
|
10
|
+
*/
|
|
11
|
+
public enum AppPlatform {
|
|
12
|
+
|
|
13
|
+
/** IOS */
|
|
14
|
+
IOS("ios"),
|
|
15
|
+
|
|
16
|
+
/** Android */
|
|
17
|
+
ANDROID("android"),
|
|
18
|
+
|
|
19
|
+
/** 鸿蒙 */
|
|
20
|
+
HARMONY("harmony"),
|
|
21
|
+
|
|
22
|
+
/** Unknown */
|
|
23
|
+
UNKNOWN("");
|
|
24
|
+
|
|
25
|
+
private final String value;
|
|
26
|
+
|
|
27
|
+
private static final Map<String, AppPlatform> VALUE_MAP = new HashMap<>();
|
|
28
|
+
|
|
29
|
+
static {
|
|
30
|
+
for (AppPlatform platform : AppPlatform.values()) {
|
|
31
|
+
VALUE_MAP.put(platform.value(), platform);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
AppPlatform(String value) {
|
|
36
|
+
this.value = value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public String value() {
|
|
40
|
+
return this.value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static boolean isIos(String platform) {
|
|
44
|
+
return IOS.value.equalsIgnoreCase(platform);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public static boolean isAndroid(String platform) {
|
|
48
|
+
return ANDROID.value.equalsIgnoreCase(platform);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public static AppPlatform findBy(String platform) {
|
|
52
|
+
if (platform == null) {
|
|
53
|
+
return UNKNOWN;
|
|
54
|
+
}
|
|
55
|
+
return VALUE_MAP.getOrDefault(platform.toLowerCase(), UNKNOWN);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package com.xiaohongshu.sns.demo.common.enums;
|
|
2
|
+
|
|
3
|
+
import lombok.AllArgsConstructor;
|
|
4
|
+
import lombok.Getter;
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @author chenhuanjie
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
@Getter
|
|
12
|
+
@AllArgsConstructor
|
|
13
|
+
public enum ErrorCodeEnum {
|
|
14
|
+
|
|
15
|
+
// 通用错误码 1000 - 1999
|
|
16
|
+
SYSTEM_ERROR(1000, "系统异常,请稍后再试"),
|
|
17
|
+
PARAM_ERROR(1001, "参数错误,请检查"),
|
|
18
|
+
PARAM_NULL(1002, "请求参数不能为空"),
|
|
19
|
+
PARAM_BLANK(1003, "必填参数不能为空"),
|
|
20
|
+
PARAM_INVALID(1004, "参数格式无效"),
|
|
21
|
+
;
|
|
22
|
+
/**
|
|
23
|
+
* 错误码
|
|
24
|
+
*/
|
|
25
|
+
private final int code;
|
|
26
|
+
/**
|
|
27
|
+
* 错误信息
|
|
28
|
+
*/
|
|
29
|
+
private final String msg;
|
|
30
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package com.xiaohongshu.sns.demo.common.exception;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import com.xiaohongshu.sns.demo.common.enums.ErrorCodeEnum;
|
|
6
|
+
import lombok.Getter;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @author chenhuanjie
|
|
11
|
+
*/
|
|
12
|
+
@Getter
|
|
13
|
+
public class AppBizException extends RuntimeException {
|
|
14
|
+
|
|
15
|
+
private int errCode;
|
|
16
|
+
|
|
17
|
+
private String errMessage;
|
|
18
|
+
|
|
19
|
+
public AppBizException(int errCode, String message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.errCode = errCode;
|
|
22
|
+
this.errMessage = message;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public AppBizException(ErrorCodeEnum err) {
|
|
26
|
+
super(err.getMsg());
|
|
27
|
+
this.errCode = err.getCode();
|
|
28
|
+
this.errMessage = err.getMsg();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public AppBizException(ErrorCodeEnum err, Throwable cause) {
|
|
32
|
+
super(err.getMsg(), cause);
|
|
33
|
+
this.errCode = err.getCode();
|
|
34
|
+
this.errMessage = err.getMsg();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
public AppBizException(int errCode, String message, Throwable cause) {
|
|
39
|
+
super(message, cause);
|
|
40
|
+
this.errCode = errCode;
|
|
41
|
+
this.errMessage = message;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Override
|
|
45
|
+
public String toString() {
|
|
46
|
+
return "AppBizException{" +
|
|
47
|
+
"errCode=" + errCode +
|
|
48
|
+
", errMessage='" + errMessage + '\'' +
|
|
49
|
+
'}';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
package com.xiaohongshu.sns.demo.common.utils;
|
|
2
|
+
|
|
3
|
+
import com.xiaohongshu.sns.demo.common.enums.ErrorCodeEnum;
|
|
4
|
+
import com.xiaohongshu.sns.demo.common.exception.AppBizException;
|
|
5
|
+
import org.apache.commons.collections4.CollectionUtils;
|
|
6
|
+
import org.apache.commons.collections4.MapUtils;
|
|
7
|
+
import org.apache.commons.lang3.StringUtils;
|
|
8
|
+
|
|
9
|
+
import java.util.Collection;
|
|
10
|
+
import java.util.Map;
|
|
11
|
+
import java.util.function.Supplier;
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @author chenhuanjie
|
|
16
|
+
*/
|
|
17
|
+
public final class AssertUtils {
|
|
18
|
+
private AssertUtils() {
|
|
19
|
+
// 私有构造函数,防止实例化
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 断言为true
|
|
24
|
+
*
|
|
25
|
+
* @param condition
|
|
26
|
+
* @param code
|
|
27
|
+
* @param supplier
|
|
28
|
+
*/
|
|
29
|
+
public static void isTrue(boolean condition, int code, Supplier<String> supplier) {
|
|
30
|
+
if (!condition) {
|
|
31
|
+
throw new AppBizException(code, supplier.get());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static void isTrue(boolean condition, ErrorCodeEnum error) {
|
|
36
|
+
if (!condition) {
|
|
37
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 断言为false
|
|
43
|
+
*
|
|
44
|
+
* @param condition
|
|
45
|
+
* @param code
|
|
46
|
+
* @param supplier
|
|
47
|
+
*/
|
|
48
|
+
public static void isFalse(boolean condition, int code, Supplier<String> supplier) {
|
|
49
|
+
if (condition) {
|
|
50
|
+
throw new AppBizException(code, supplier.get());
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public static void isFalse(boolean condition, ErrorCodeEnum error) {
|
|
55
|
+
if (condition) {
|
|
56
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 断言为字符串非空
|
|
62
|
+
*
|
|
63
|
+
* @param s
|
|
64
|
+
* @param code
|
|
65
|
+
* @param supplier
|
|
66
|
+
*/
|
|
67
|
+
public static void notBlank(String s, int code, Supplier<String> supplier) {
|
|
68
|
+
if (StringUtils.isBlank(s)) {
|
|
69
|
+
throw new AppBizException(code, supplier.get());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public static void notBlank(String s, ErrorCodeEnum error) {
|
|
74
|
+
if (StringUtils.isBlank(s)) {
|
|
75
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 断言为集合非空
|
|
82
|
+
*
|
|
83
|
+
* @param c
|
|
84
|
+
* @param code
|
|
85
|
+
* @param supplier
|
|
86
|
+
*/
|
|
87
|
+
public static void notEmpty(Collection c, int code, Supplier<String> supplier) {
|
|
88
|
+
if (CollectionUtils.isEmpty(c)) {
|
|
89
|
+
throw new AppBizException(code, supplier.get());
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public static void notEmpty(Collection m, ErrorCodeEnum error) {
|
|
94
|
+
if (CollectionUtils.isEmpty(m)) {
|
|
95
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 断言map非空
|
|
101
|
+
*
|
|
102
|
+
* @param m
|
|
103
|
+
* @param code
|
|
104
|
+
* @param supplier
|
|
105
|
+
*/
|
|
106
|
+
public static void notEmpty(Map m, int code, Supplier<String> supplier) {
|
|
107
|
+
if (MapUtils.isEmpty(m)) {
|
|
108
|
+
throw new AppBizException(code, supplier.get());
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public static void notEmpty(Map m, ErrorCodeEnum error) {
|
|
113
|
+
if (MapUtils.isEmpty(m)) {
|
|
114
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 断言为对象非空
|
|
121
|
+
*
|
|
122
|
+
* @param o
|
|
123
|
+
* @param code
|
|
124
|
+
* @param supplier
|
|
125
|
+
*/
|
|
126
|
+
public static void notNull(Object o, int code, Supplier<String> supplier) {
|
|
127
|
+
if (o == null) {
|
|
128
|
+
throw new AppBizException(code, supplier.get());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public static void notNull(Object o, ErrorCodeEnum error) {
|
|
133
|
+
if (o == null) {
|
|
134
|
+
throw new AppBizException(error.getCode(), error.getMsg());
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/JsonHelper.java
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
package com.xiaohongshu.sns.demo.common.utils;
|
|
2
|
+
|
|
3
|
+
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
4
|
+
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
5
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
6
|
+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
|
7
|
+
import com.fasterxml.jackson.databind.type.CollectionType;
|
|
8
|
+
import com.fasterxml.jackson.databind.type.MapType;
|
|
9
|
+
import lombok.extern.slf4j.Slf4j;
|
|
10
|
+
|
|
11
|
+
import java.util.List;
|
|
12
|
+
import java.util.Map;
|
|
13
|
+
import java.util.Set;
|
|
14
|
+
|
|
15
|
+
@Slf4j
|
|
16
|
+
public final class JsonHelper {
|
|
17
|
+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
18
|
+
private static final ObjectMapper SNAKE_CASE_OBJECT_MAPPER = new ObjectMapper();
|
|
19
|
+
|
|
20
|
+
static {
|
|
21
|
+
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
22
|
+
OBJECT_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
|
|
23
|
+
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
|
|
24
|
+
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
25
|
+
|
|
26
|
+
SNAKE_CASE_OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
27
|
+
SNAKE_CASE_OBJECT_MAPPER.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy());
|
|
28
|
+
SNAKE_CASE_OBJECT_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
|
|
29
|
+
SNAKE_CASE_OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
|
|
30
|
+
SNAKE_CASE_OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 将对象转换为 JSON 字符串
|
|
36
|
+
*
|
|
37
|
+
* @param object
|
|
38
|
+
* @param snakeCase
|
|
39
|
+
* @param <T>
|
|
40
|
+
* @return
|
|
41
|
+
*/
|
|
42
|
+
public static <T> String toJson(T object, boolean snakeCase) {
|
|
43
|
+
try {
|
|
44
|
+
ObjectMapper mapper = snakeCase ? SNAKE_CASE_OBJECT_MAPPER : OBJECT_MAPPER;
|
|
45
|
+
return mapper.writeValueAsString(object);
|
|
46
|
+
} catch (Exception e) {
|
|
47
|
+
log.error("Error converting object to JSON: {}", e.getMessage(), e);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 将 JSON 字符串转换为对象
|
|
54
|
+
*
|
|
55
|
+
* @param json
|
|
56
|
+
* @param clazz
|
|
57
|
+
* @param snakeCase
|
|
58
|
+
* @param <T>
|
|
59
|
+
* @return
|
|
60
|
+
*/
|
|
61
|
+
public static <T> T fromJson(String json, Class<T> clazz, boolean snakeCase) {
|
|
62
|
+
try {
|
|
63
|
+
ObjectMapper mapper = snakeCase ? SNAKE_CASE_OBJECT_MAPPER : OBJECT_MAPPER;
|
|
64
|
+
return mapper.readValue(json, clazz);
|
|
65
|
+
} catch (Exception e) {
|
|
66
|
+
log.error("Error converting JSON to object: {}", e.getMessage(), e);
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 将 JSON 字符串转换为 List
|
|
73
|
+
*
|
|
74
|
+
* @param json
|
|
75
|
+
* @param clazz
|
|
76
|
+
* @param <T>
|
|
77
|
+
* @return
|
|
78
|
+
*/
|
|
79
|
+
public static <T> List<T> fromJsonToList(String json, Class<T> clazz, boolean snakeCase) {
|
|
80
|
+
try {
|
|
81
|
+
ObjectMapper mapper = snakeCase ? SNAKE_CASE_OBJECT_MAPPER : OBJECT_MAPPER;
|
|
82
|
+
CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
|
|
83
|
+
return mapper.readValue(json, type);
|
|
84
|
+
} catch (Exception e) {
|
|
85
|
+
log.error("Error converting JSON to List: {}", e.getMessage(), e);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 将 JSON 字符串转换为 List
|
|
92
|
+
*
|
|
93
|
+
* @param json
|
|
94
|
+
* @param clazz
|
|
95
|
+
* @param <T>
|
|
96
|
+
* @return
|
|
97
|
+
*/
|
|
98
|
+
public static <T> Set<T> fromJsonToSet(String json, Class<T> clazz, boolean snakeCase) {
|
|
99
|
+
try {
|
|
100
|
+
ObjectMapper mapper = snakeCase ? SNAKE_CASE_OBJECT_MAPPER : OBJECT_MAPPER;
|
|
101
|
+
CollectionType type = mapper.getTypeFactory().constructCollectionType(Set.class, clazz);
|
|
102
|
+
return mapper.readValue(json, type);
|
|
103
|
+
} catch (Exception e) {
|
|
104
|
+
log.error("Error converting JSON to Set: {}", e.getMessage(), e);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 将 JSON 字符串转换为 Map
|
|
111
|
+
*
|
|
112
|
+
* @param json
|
|
113
|
+
* @param keyClass
|
|
114
|
+
* @param valueClass
|
|
115
|
+
* @param <K>
|
|
116
|
+
* @param <V>
|
|
117
|
+
* @return
|
|
118
|
+
*/
|
|
119
|
+
public static <K, V> Map<K, V> fromJsonToMap(String json, Class<K> keyClass, Class<V> valueClass, boolean snakeCase) {
|
|
120
|
+
try {
|
|
121
|
+
ObjectMapper mapper = snakeCase ? SNAKE_CASE_OBJECT_MAPPER : OBJECT_MAPPER;
|
|
122
|
+
MapType type = mapper.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
|
|
123
|
+
return mapper.readValue(json, type);
|
|
124
|
+
} catch (Exception e) {
|
|
125
|
+
log.error("Error converting JSON to Map: {}", e.getMessage(), e);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|