Skip to content

Commit dfe354e

Browse files
committed
升级 APIJSON 8,移除 fastjson
1 parent ced3fa6 commit dfe354e

File tree

8 files changed

+221
-191
lines changed

8 files changed

+221
-191
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONB
7676
```java
7777
@RestController
7878
@RequestMapping("")
79-
public class DemoController extends APIJSONRouterController<Long> {
79+
public class DemoController extends APIJSONRouterController<Long, JSONObject, JSONArray> {
8080
}
8181
```
8282
![image](https://user-images.githubusercontent.com/5738175/167263296-3bfd8782-c163-4461-bbed-f264be529e76.png)

pom.xml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,17 @@
3535
<scope>provided</scope>
3636
</dependency>
3737

38-
<dependency>
39-
<groupId>com.alibaba</groupId>
40-
<artifactId>fastjson</artifactId>
41-
<version>1.2.83</version>
42-
</dependency>
43-
4438
<!-- 可使用 libs 目录的 apijson-orm.jar 来替代,两种方式二选一 -->
45-
<dependency>
46-
<groupId>com.github.Tencent</groupId>
47-
<artifactId>APIJSON</artifactId>
48-
<version>7.5.5</version>
49-
</dependency>
50-
<dependency>
51-
<groupId>com.github.APIJSON</groupId>
52-
<artifactId>apijson-framework</artifactId>
53-
<version>7.1.7</version>
54-
</dependency>
39+
<!-- <dependency>-->
40+
<!-- <groupId>com.github.Tencent</groupId>-->
41+
<!-- <artifactId>APIJSON</artifactId>-->
42+
<!-- <version>8.0.0.0.0</version>-->
43+
<!-- </dependency>-->
44+
<!-- <dependency>-->
45+
<!-- <groupId>com.github.APIJSON</groupId>-->
46+
<!-- <artifactId>apijson-framework</artifactId>-->
47+
<!-- <version>7.2.0.0</version>-->
48+
<!-- </dependency>-->
5549
</dependencies>
5650

5751
<build>

src/main/java/apijson/router/APIJSONRouterApplication.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import apijson.framework.APIJSONApplication;
1919
import apijson.framework.APIJSONCreator;
2020

21+
import java.util.List;
22+
import java.util.Map;
23+
2124

2225
/**启动入口 Application 基类
2326
* 右键这个类 > Run As > Java Application
@@ -46,7 +49,8 @@ public static void init(boolean shutdownWhenServerError) throws Exception {
4649
* @return
4750
* @throws Exception
4851
*/
49-
public static <T extends Object> void init(@NotNull APIJSONCreator<T> creator) throws Exception {
52+
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(
53+
@NotNull APIJSONCreator<T, M, L> creator) throws Exception {
5054
init(true, creator);
5155
}
5256
/**初始化,加载所有配置并校验
@@ -55,7 +59,8 @@ public static <T extends Object> void init(@NotNull APIJSONCreator<T> creator) t
5559
* @return
5660
* @throws Exception
5761
*/
58-
public static <T extends Object> void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator<T> creator) throws Exception {
62+
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(boolean shutdownWhenServerError
63+
, @NotNull APIJSONCreator<T, M, L> creator) throws Exception {
5964
// 避免多个插件重复调用这句 APIJSONApplication.init(shutdownWhenServerError, creator);
6065
System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON Router 开始启动 >>>>>>>>>>>>>>>>>>>>>>>>\n");
6166

src/main/java/apijson/router/APIJSONRouterController.java

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,16 @@
1414

1515
package apijson.router;
1616

17+
import static apijson.JSON.getJSONObject;
18+
import static apijson.JSON.getString;
1719
import static apijson.RequestMethod.GET;
1820
import static apijson.framework.APIJSONConstant.METHODS;
1921

20-
import java.util.Arrays;
21-
import java.util.HashMap;
22-
import java.util.Map;
22+
import java.util.*;
2323
import java.util.Map.Entry;
24-
import java.util.Set;
25-
import java.util.SortedMap;
26-
import java.util.TreeMap;
2724

2825
import jakarta.servlet.http.HttpSession;
2926

30-
import com.alibaba.fastjson.JSONObject;
31-
3227
import apijson.JSON;
3328
import apijson.JSONRequest;
3429
import apijson.Log;
@@ -47,7 +42,8 @@
4742
/**APIJSON router controller,建议在子项目被 @RestController 注解的类继承它或通过它的实例调用相关方法
4843
* @author Lemon
4944
*/
50-
public class APIJSONRouterController<T extends Object> extends APIJSONController<T> {
45+
public class APIJSONRouterController<T, M extends Map<String, Object>, L extends List<Object>>
46+
extends APIJSONController<T, M, L> {
5147
public static final String TAG = "APIJSONRouterController";
5248

5349
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -74,44 +70,65 @@ public String router(String method, String tag, Map<String, String> params, Stri
7470
* @return
7571
*/
7672
public String router(String method, String tag, Map<String, String> params, String request, HttpSession session, boolean compatCommonAPI) {
73+
RequestMethod requestMethod = null;
74+
try {
75+
requestMethod = RequestMethod.valueOf(method.toUpperCase());
76+
} catch (Throwable e) {
77+
// 下方 METHODS.contains(method) 会抛异常
78+
}
79+
Parser<T, M, L> parser = newParser(session, requestMethod);
80+
7781
if (METHODS.contains(method) == false) {
78-
return APIJSONParser.newErrorResult(new IllegalArgumentException("URL 路径 /{method}/{tag} 中 method 值 " + method
79-
+ " 错误!只允许 " + METHODS + " 中的一个!")).toJSONString();
82+
return JSON.toJSONString(
83+
parser.newErrorResult(
84+
new IllegalArgumentException("URL 路径 /{method}/{tag} 中 method 值 "
85+
+ method + " 错误!只允许 " + METHODS + " 中的一个!"
86+
)
87+
)
88+
);
8089
}
81-
90+
8291
String t = compatCommonAPI && tag != null && tag.endsWith("[]") ? tag.substring(0, tag.length() - 2) : tag;
8392
if (StringUtil.isName(t) == false) {
84-
return APIJSONParser.newErrorResult(new IllegalArgumentException("URL 路径 /" + method + "/{tag} 的 tag 中 " + t
85-
+ " 错误!tag 不能为空,且只允许变量命名格式!")).toJSONString();
93+
return JSON.toJSONString(
94+
parser.newErrorResult(
95+
new IllegalArgumentException("URL 路径 /" + method + "/{tag} 的 tag 中 "
96+
+ t + " 错误!tag 不能为空,且只允许变量命名格式!"
97+
)
98+
)
99+
);
86100
}
87101

88102
String versionStr = params == null ? null : params.remove(APIJSONConstant.VERSION);
89103
Integer version;
90104
try {
91105
version = StringUtil.isEmpty(versionStr, false) ? null : Integer.valueOf(versionStr);
92-
}
106+
}
93107
catch (Exception e) {
94-
return APIJSONParser.newErrorResult(new IllegalArgumentException("URL 路径 /" + method
95-
+ "/" + tag + "?version=value 中 value 值 " + versionStr + " 错误!必须符合整数格式!")).toJSONString();
108+
return JSON.toJSONString(
109+
parser.newErrorResult(new IllegalArgumentException("URL 路径 /" + method + "/"
110+
+ tag + "?version=value 中 value 值 " + versionStr + " 错误!必须符合整数格式!")
111+
)
112+
);
96113
}
97114

98115
if (version == null) {
99116
version = 0;
100117
}
101118

102119
try {
103-
// 从 Document 查这样的接口
120+
// 从 Document 查这样的接口
104121
String cacheKey = AbstractVerifier.getCacheKeyForRequest(method, tag);
105-
SortedMap<Integer, JSONObject> versionedMap = APIJSONRouterVerifier.DOCUMENT_MAP.get(cacheKey);
122+
SortedMap<Integer, Map<String, Object>> versionedMap = APIJSONRouterVerifier.DOCUMENT_MAP.get(cacheKey);
106123

107-
JSONObject result = versionedMap == null ? null : versionedMap.get(version);
124+
Map<String, Object> result = versionedMap == null ? null : versionedMap.get(version);
108125
if (result == null) { // version <= 0 时使用最新,version > 0 时使用 > version 的最接近版本(最小版本)
109-
Set<Entry<Integer, JSONObject>> set = versionedMap == null ? null : versionedMap.entrySet();
126+
Set<Entry<Integer, Map<String, Object>>> set = versionedMap == null ? null : versionedMap.entrySet();
110127

111128
if (set != null && set.isEmpty() == false) {
112-
Entry<Integer, JSONObject> maxEntry = null;
129+
Entry<Integer, Map<String, Object>> maxEntry = null;
113130

114-
for (Entry<Integer, JSONObject> entry : set) {
131+
for (Entry<Integer, Map<String, Object>> entry : set) {
115132
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
116133
continue;
117134
}
@@ -144,11 +161,11 @@ public String router(String method, String tag, Map<String, String> params, Stri
144161
}
145162

146163
@SuppressWarnings("unchecked")
147-
APIJSONCreator<T> creator = (APIJSONCreator<T>) APIJSONParser.APIJSON_CREATOR;
164+
APIJSONCreator<T, M, L> creator = (APIJSONCreator<T, M, L>) APIJSONParser.APIJSON_CREATOR;
148165
if (result == null && Log.DEBUG && APIJSONRouterVerifier.DOCUMENT_MAP.isEmpty()) {
149166

150167
//获取指定的JSON结构 <<<<<<<<<<<<<<
151-
SQLConfig config = creator.createSQLConfig().setMethod(GET).setTable(APIJSONConstant.DOCUMENT_);
168+
SQLConfig<T, M, L> config = creator.createSQLConfig().setMethod(GET).setTable(APIJSONConstant.DOCUMENT_);
152169
config.setPrepared(false);
153170
config.setColumn(Arrays.asList("request,apijson"));
154171

@@ -171,7 +188,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
171188
// DOCUMENT_MAP.put(cacheKey, versionedMap);
172189
}
173190

174-
String apijson = result == null ? null : result.getString("apijson");
191+
String apijson = result == null ? null : getString(result, "apijson");
175192
if (StringUtil.isEmpty(apijson, true)) { //
176193
if (compatCommonAPI) {
177194
return crudByTag(method, tag, params, request, session);
@@ -181,37 +198,31 @@ public String router(String method, String tag, Map<String, String> params, Stri
181198
+ "/" + tag + (versionStr == null ? "" : "?version=" + versionStr) + " 对应的接口不存在!");
182199
}
183200

184-
JSONObject rawReq = JSON.parseObject(request);
201+
M rawReq = JSON.parseObject(request);
185202
if (rawReq == null) {
186-
rawReq = new JSONObject(true);
203+
rawReq = JSON.createJSONObject();
187204
}
188205
if (params != null && params.isEmpty() == false) {
189206
rawReq.putAll(params);
190207
}
191208

192-
RequestMethod requestMethod = RequestMethod.valueOf(method.toUpperCase());
193-
Parser<T> parser = newParser(session, requestMethod);
194-
195209
if (parser.isNeedVerifyContent()) {
196-
Verifier<T> verifier = creator.createVerifier();
210+
Verifier<T, M, L> verifier = creator.createVerifier();
197211

198212
//获取指定的JSON结构 <<<<<<<<<<<<
199-
JSONObject object;
200-
object = parser.getStructure("Request", method.toUpperCase(), tag, version);
201-
if (object == null) { //empty表示随意操作 || object.isEmpty()) {
213+
Map<String, Object> target = parser.getStructure("Request", method.toUpperCase(), tag, version);
214+
if (target == null) { //empty表示随意操作 || object.isEmpty()) {
202215
throw new UnsupportedOperationException("找不到 version: " + version + ", method: " + method.toUpperCase() + ", tag: " + tag + " 对应的 structure !"
203216
+ "非开放请求必须是后端 Request 表中校验规则允许的操作!如果需要则在 Request 表中新增配置!");
204217
}
205218

206-
JSONObject target = object;
207-
208-
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
209-
verifier.verifyRequest(requestMethod, "", target, rawReq, 0, null, null, creator);
219+
//M clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
220+
verifier.verifyRequest(requestMethod, "", JSON.createJSONObject(target), rawReq, 0, null, null, creator);
210221
}
211222

212-
JSONObject apijsonReq = JSON.parseObject(apijson);
223+
M apijsonReq = JSON.parseObject(apijson);
213224
if (apijsonReq == null) {
214-
apijsonReq = new JSONObject(true);
225+
apijsonReq = JSON.createJSONObject();
215226
}
216227

217228
Set<Entry<String, Object>> rawSet = rawReq.entrySet();
@@ -225,11 +236,11 @@ public String router(String method, String tag, Map<String, String> params, Stri
225236
String[] pathKeys = key.split("\\.");
226237
//逐层到达child的直接容器JSONObject parent
227238
int last = pathKeys.length - 1;
228-
JSONObject parent = apijsonReq;
239+
M parent = apijsonReq;
229240
for (int i = 0; i < last; i++) {//一步一步到达指定位置
230-
JSONObject p = parent.getJSONObject(pathKeys[i]);
241+
M p = getJSONObject(parent, pathKeys[i]);
231242
if (p == null) {
232-
p = new JSONObject(true);
243+
p = JSON.createJSONObject();
233244
parent.put(key, p);
234245
}
235246
parent = p;
@@ -244,7 +255,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
244255
return parser.setNeedVerifyContent(false).parse(apijsonReq);
245256
}
246257
catch (Exception e) {
247-
return APIJSONParser.newErrorResult(e).toJSONString();
258+
return JSON.toJSONString(parser.newErrorResult(e));
248259
}
249260
}
250261

0 commit comments

Comments
 (0)