18
18
import apijson .JSONRequest ;
19
19
import apijson .orm .*;
20
20
21
- import apijson .orm .exception .CommonException ;
22
21
import jakarta .servlet .http .HttpSession ;
23
22
24
23
import java .rmi .ServerException ;
@@ -51,7 +50,7 @@ public APIJSONParser<T, M, L> newParser(HttpSession session, RequestMethod metho
51
50
return parser ;
52
51
}
53
52
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 ();
55
54
56
55
/**新建带状态内容的JSONObject
57
56
* @param code
@@ -96,7 +95,7 @@ public static <M extends Map<String, Object>> M newResult(int code, String msg,
96
95
* @return
97
96
*/
98
97
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 );
100
99
}
101
100
102
101
@@ -169,23 +168,43 @@ public static <M extends Map<String, Object>> M extendErrorResult(M object, Thro
169
168
* @return
170
169
*/
171
170
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 );
173
172
}
174
173
175
174
public static <M extends Map <String , Object >> M newErrorResult (Exception e ) {
176
175
return newErrorResult (e , false );
177
176
}
178
177
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 );
180
179
}
181
180
182
181
183
182
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
+
184
193
return newParser (session , method ).parse (request );
185
194
}
186
195
187
196
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 );
189
208
M req = parser .wrapRequest (method , tag , JSON .parseObject (request ), false );
190
209
if (req == null ) {
191
210
req = JSON .createJSONObject ();
@@ -194,11 +213,20 @@ public String parseByTag(RequestMethod method, String tag, Map<String, String> p
194
213
req .putAll (params );
195
214
}
196
215
197
- return newParser ( session , method ) .parse (req );
216
+ return parser .parse (req );
198
217
}
199
218
200
219
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
201
220
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
+
202
230
/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一点路由解析性能来提升一些开发效率
203
231
* @param method
204
232
* @param request
@@ -210,8 +238,7 @@ public String crud(String method, String request, HttpSession session) {
210
238
return parse (RequestMethod .valueOf (method .toUpperCase ()), request , session );
211
239
}
212
240
213
- Parser <T , M , L > parser = newParser (null , null );
214
- return toJSONString (parser .newErrorResult (
241
+ return toJSONString (newErrorResult (
215
242
new IllegalArgumentException ("URL 路径 /{method} 中 method 值 "
216
243
+ method + " 错误!只允许 " + METHODS + " 中的一个!" )
217
244
));
@@ -316,8 +343,7 @@ public String crudByTag(String method, String tag, Map<String, String> params, S
316
343
return parseByTag (RequestMethod .valueOf (method .toUpperCase ()), tag , params , request , session );
317
344
}
318
345
319
- Parser <T , M , L > parser = newParser (null , null );
320
- return toJSONString (parser .newErrorResult (
346
+ return toJSONString (newErrorResult (
321
347
new IllegalArgumentException ("URL 路径 /{method}/{tag} 中 method 值 "
322
348
+ method + " 错误!只允许 " + METHODS + " 中的一个!" )
323
349
));
@@ -428,6 +454,15 @@ public String router(String method, String tag, Map<String, String> params, Stri
428
454
* @return
429
455
*/
430
456
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
+
431
466
RequestMethod requestMethod = null ;
432
467
try {
433
468
requestMethod = RequestMethod .valueOf (method .toUpperCase ());
@@ -438,7 +473,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
438
473
439
474
if (METHODS .contains (method ) == false ) {
440
475
return JSON .toJSONString (
441
- parser . newErrorResult (
476
+ newErrorResult (
442
477
new IllegalArgumentException ("URL 路径 /{method}/{tag} 中 method 值 "
443
478
+ method + " 错误!只允许 " + METHODS + " 中的一个!"
444
479
)
@@ -449,7 +484,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
449
484
String t = compatCommonAPI && tag != null && tag .endsWith ("[]" ) ? tag .substring (0 , tag .length () - 2 ) : tag ;
450
485
if (StringUtil .isName (t ) == false ) {
451
486
return JSON .toJSONString (
452
- parser . newErrorResult (
487
+ newErrorResult (
453
488
new IllegalArgumentException ("URL 路径 /" + method + "/{tag} 的 tag 中 "
454
489
+ t + " 错误!tag 不能为空,且只允许变量命名格式!"
455
490
)
@@ -464,7 +499,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
464
499
}
465
500
catch (Exception e ) {
466
501
return JSON .toJSONString (
467
- parser . newErrorResult (new IllegalArgumentException ("URL 路径 /" + method + "/"
502
+ newErrorResult (new IllegalArgumentException ("URL 路径 /" + method + "/"
468
503
+ tag + "?version=value 中 value 值 " + versionStr + " 错误!必须符合整数格式!" )
469
504
)
470
505
);
@@ -613,7 +648,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
613
648
return parser .setNeedVerifyContent (false ).parse (apijsonReq );
614
649
}
615
650
catch (Exception e ) {
616
- return JSON .toJSONString (parser . newErrorResult (e ));
651
+ return JSON .toJSONString (newErrorResult (e ));
617
652
}
618
653
}
619
654
@@ -634,8 +669,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
634
669
* </pre>
635
670
*/
636
671
public M reload (String type ) {
637
- Parser <T , M , L > parser = newParser (null , null );
638
- M result = parser .newSuccessResult ();
672
+ M result = newSuccessResult ();
639
673
640
674
boolean reloadAll = StringUtil .isEmpty (type , true ) || "ALL" .equals (type );
641
675
@@ -651,7 +685,7 @@ public M reload(String type) {
651
685
}
652
686
} catch (ServerException e ) {
653
687
e .printStackTrace ();
654
- result .put (ACCESS_ , parser . newErrorResult (e ));
688
+ result .put (ACCESS_ , newErrorResult (e ));
655
689
}
656
690
}
657
691
@@ -667,7 +701,7 @@ public M reload(String type) {
667
701
}
668
702
} catch (ServerException e ) {
669
703
e .printStackTrace ();
670
- result .put (FUNCTION_ , parser . newErrorResult (e ));
704
+ result .put (FUNCTION_ , newErrorResult (e ));
671
705
}
672
706
}
673
707
@@ -683,7 +717,7 @@ public M reload(String type) {
683
717
}
684
718
} catch (ServerException e ) {
685
719
e .printStackTrace ();
686
- result .put (REQUEST_ , parser . newErrorResult (e ));
720
+ result .put (REQUEST_ , newErrorResult (e ));
687
721
}
688
722
}
689
723
0 commit comments