Skip to content

Commit 60d88a6

Browse files
committed
新增 /crud 全能增删改查统一入口,按 ENABLE_APIJSON_ROUTER 及 Log.DEBUG 控制是否支持 /router/{method}/{tag} 及 /get 等万能通用接口
1 parent 583b369 commit 60d88a6

File tree

2 files changed

+237
-37
lines changed

2 files changed

+237
-37
lines changed

src/main/java/apijson/framework/APIJSONController.java

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import apijson.JSONRequest;
1919
import apijson.orm.*;
2020

21-
import apijson.orm.exception.CommonException;
2221
import jakarta.servlet.http.HttpSession;
2322

2423
import java.rmi.ServerException;
@@ -51,7 +50,7 @@ public APIJSONParser<T, M, L> newParser(HttpSession session, RequestMethod metho
5150
return parser;
5251
}
5352

54-
public static APIJSONParser<?, ? extends Map<String, Object>, ? extends List<Object>> ERR_PARSER = APIJSONApplication.createParser();
53+
public static APIJSONParser<?, ? extends Map<String, Object>, ? extends List<Object>> COMMON_PARSER = APIJSONApplication.createParser();
5554

5655
/**新建带状态内容的JSONObject
5756
* @param code
@@ -96,7 +95,7 @@ public static <M extends Map<String, Object>> M newResult(int code, String msg,
9695
* @return
9796
*/
9897
public static <M extends Map<String, Object>> M extendResult(M object, int code, String msg, String warn, boolean isRoot) {
99-
return (M) ERR_PARSER.extendResult(JSON.createJSONObject(object), code, msg, warn, isRoot);
98+
return (M) COMMON_PARSER.extendResult(JSON.createJSONObject(object), code, msg, warn, isRoot);
10099
}
101100

102101

@@ -169,23 +168,43 @@ public static <M extends Map<String, Object>> M extendErrorResult(M object, Thro
169168
* @return
170169
*/
171170
public static <M extends Map<String, Object>> M extendErrorResult(M object, Throwable e, RequestMethod requestMethod, String url, boolean isRoot) {
172-
return (M) ERR_PARSER.extendErrorResult(JSON.createJSONObject(object), e, requestMethod, url, isRoot);
171+
return (M) COMMON_PARSER.extendErrorResult(JSON.createJSONObject(object), e, requestMethod, url, isRoot);
173172
}
174173

175174
public static <M extends Map<String, Object>> M newErrorResult(Exception e) {
176175
return newErrorResult(e, false);
177176
}
178177
public static <M extends Map<String, Object>> M newErrorResult(Exception e, boolean isRoot) {
179-
return (M) ERR_PARSER.newErrorResult(e, isRoot);
178+
return (M) COMMON_PARSER.newErrorResult(e, isRoot);
180179
}
181180

182181

183182
public String parse(RequestMethod method, String request, HttpSession session) {
183+
if (APIJSONVerifier.ENABLE_APIJSON_ROUTER && ! Log.DEBUG) {
184+
return JSON.toJSONString(
185+
newErrorResult(
186+
new IllegalArgumentException("APIJSONVerifier.ENABLE_APIJSON_ROUTER = true 已启用 router," +
187+
"Log.DEBUG = false 时不允许调用 /router/{method}/{tag} 外的万能通用接口!"
188+
)
189+
)
190+
);
191+
}
192+
184193
return newParser(session, method).parse(request);
185194
}
186195

187196
public String parseByTag(RequestMethod method, String tag, Map<String, String> params, String request, HttpSession session) {
188-
APIJSONParser<T, M, L> parser = newParser(null, null);
197+
if (APIJSONVerifier.ENABLE_APIJSON_ROUTER && ! Log.DEBUG) {
198+
return JSON.toJSONString(
199+
newErrorResult(
200+
new IllegalArgumentException("APIJSONVerifier.ENABLE_APIJSON_ROUTER = true 已启用 router," +
201+
"Log.DEBUG = false 时不允许调用 /router/{method}/{tag} 外的万能通用接口!"
202+
)
203+
)
204+
);
205+
}
206+
207+
APIJSONParser<T, M, L> parser = newParser(session, method);
189208
M req = parser.wrapRequest(method, tag, JSON.parseObject(request), false);
190209
if (req == null) {
191210
req = JSON.createJSONObject();
@@ -194,11 +213,20 @@ public String parseByTag(RequestMethod method, String tag, Map<String, String> p
194213
req.putAll(params);
195214
}
196215

197-
return newParser(session, method).parse(req);
216+
return parser.parse(req);
198217
}
199218

200219
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
201220

221+
/**全能增删改查统一入口,这个一个方法可替代以下所有万能通用方法,一个接口通用增删改查
222+
* @param request
223+
* @param session
224+
* @return
225+
*/
226+
public String crudAll(String request, HttpSession session) {
227+
return parse(CRUD, request, session);
228+
}
229+
202230
/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一点路由解析性能来提升一些开发效率
203231
* @param method
204232
* @param request
@@ -210,8 +238,7 @@ public String crud(String method, String request, HttpSession session) {
210238
return parse(RequestMethod.valueOf(method.toUpperCase()), request, session);
211239
}
212240

213-
Parser<T, M, L> parser = newParser(null, null);
214-
return toJSONString(parser.newErrorResult(
241+
return toJSONString(newErrorResult(
215242
new IllegalArgumentException("URL 路径 /{method} 中 method 值 "
216243
+ method + " 错误!只允许 " + METHODS + " 中的一个!")
217244
));
@@ -316,8 +343,7 @@ public String crudByTag(String method, String tag, Map<String, String> params, S
316343
return parseByTag(RequestMethod.valueOf(method.toUpperCase()), tag, params, request, session);
317344
}
318345

319-
Parser<T, M, L> parser = newParser(null, null);
320-
return toJSONString(parser.newErrorResult(
346+
return toJSONString(newErrorResult(
321347
new IllegalArgumentException("URL 路径 /{method}/{tag} 中 method 值 "
322348
+ method + " 错误!只允许 " + METHODS + " 中的一个!")
323349
));
@@ -428,6 +454,15 @@ public String router(String method, String tag, Map<String, String> params, Stri
428454
* @return
429455
*/
430456
public String router(String method, String tag, Map<String, String> params, String request, HttpSession session, boolean compatCommonAPI) {
457+
if (! APIJSONVerifier.ENABLE_APIJSON_ROUTER) {
458+
return JSON.toJSONString(
459+
newErrorResult(
460+
new IllegalArgumentException("未启用 router!请配置 APIJSONVerifier.ENABLE_APIJSON_ROUTER = true !"
461+
)
462+
)
463+
);
464+
}
465+
431466
RequestMethod requestMethod = null;
432467
try {
433468
requestMethod = RequestMethod.valueOf(method.toUpperCase());
@@ -438,7 +473,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
438473

439474
if (METHODS.contains(method) == false) {
440475
return JSON.toJSONString(
441-
parser.newErrorResult(
476+
newErrorResult(
442477
new IllegalArgumentException("URL 路径 /{method}/{tag} 中 method 值 "
443478
+ method + " 错误!只允许 " + METHODS + " 中的一个!"
444479
)
@@ -449,7 +484,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
449484
String t = compatCommonAPI && tag != null && tag.endsWith("[]") ? tag.substring(0, tag.length() - 2) : tag;
450485
if (StringUtil.isName(t) == false) {
451486
return JSON.toJSONString(
452-
parser.newErrorResult(
487+
newErrorResult(
453488
new IllegalArgumentException("URL 路径 /" + method + "/{tag} 的 tag 中 "
454489
+ t + " 错误!tag 不能为空,且只允许变量命名格式!"
455490
)
@@ -464,7 +499,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
464499
}
465500
catch (Exception e) {
466501
return JSON.toJSONString(
467-
parser.newErrorResult(new IllegalArgumentException("URL 路径 /" + method + "/"
502+
newErrorResult(new IllegalArgumentException("URL 路径 /" + method + "/"
468503
+ tag + "?version=value 中 value 值 " + versionStr + " 错误!必须符合整数格式!")
469504
)
470505
);
@@ -613,7 +648,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
613648
return parser.setNeedVerifyContent(false).parse(apijsonReq);
614649
}
615650
catch (Exception e) {
616-
return JSON.toJSONString(parser.newErrorResult(e));
651+
return JSON.toJSONString(newErrorResult(e));
617652
}
618653
}
619654

@@ -634,8 +669,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
634669
* </pre>
635670
*/
636671
public M reload(String type) {
637-
Parser<T, M, L> parser = newParser(null, null);
638-
M result = parser.newSuccessResult();
672+
M result = newSuccessResult();
639673

640674
boolean reloadAll = StringUtil.isEmpty(type, true) || "ALL".equals(type);
641675

@@ -651,7 +685,7 @@ public M reload(String type) {
651685
}
652686
} catch (ServerException e) {
653687
e.printStackTrace();
654-
result.put(ACCESS_, parser.newErrorResult(e));
688+
result.put(ACCESS_, newErrorResult(e));
655689
}
656690
}
657691

@@ -667,7 +701,7 @@ public M reload(String type) {
667701
}
668702
} catch (ServerException e) {
669703
e.printStackTrace();
670-
result.put(FUNCTION_, parser.newErrorResult(e));
704+
result.put(FUNCTION_, newErrorResult(e));
671705
}
672706
}
673707

@@ -683,7 +717,7 @@ public M reload(String type) {
683717
}
684718
} catch (ServerException e) {
685719
e.printStackTrace();
686-
result.put(REQUEST_, parser.newErrorResult(e));
720+
result.put(REQUEST_, newErrorResult(e));
687721
}
688722
}
689723

0 commit comments

Comments
 (0)