14
14
15
15
package apijson .router ;
16
16
17
+ import static apijson .JSON .getJSONObject ;
18
+ import static apijson .JSON .getString ;
17
19
import static apijson .RequestMethod .GET ;
18
20
import static apijson .framework .APIJSONConstant .METHODS ;
19
21
20
- import java .util .Arrays ;
21
- import java .util .HashMap ;
22
- import java .util .Map ;
22
+ import java .util .*;
23
23
import java .util .Map .Entry ;
24
- import java .util .Set ;
25
- import java .util .SortedMap ;
26
- import java .util .TreeMap ;
27
24
28
25
import jakarta .servlet .http .HttpSession ;
29
26
30
- import com .alibaba .fastjson .JSONObject ;
31
-
32
27
import apijson .JSON ;
33
28
import apijson .JSONRequest ;
34
29
import apijson .Log ;
47
42
/**APIJSON router controller,建议在子项目被 @RestController 注解的类继承它或通过它的实例调用相关方法
48
43
* @author Lemon
49
44
*/
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 > {
51
47
public static final String TAG = "APIJSONRouterController" ;
52
48
53
49
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -74,44 +70,65 @@ public String router(String method, String tag, Map<String, String> params, Stri
74
70
* @return
75
71
*/
76
72
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
+
77
81
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
+ );
80
89
}
81
-
90
+
82
91
String t = compatCommonAPI && tag != null && tag .endsWith ("[]" ) ? tag .substring (0 , tag .length () - 2 ) : tag ;
83
92
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
+ );
86
100
}
87
101
88
102
String versionStr = params == null ? null : params .remove (APIJSONConstant .VERSION );
89
103
Integer version ;
90
104
try {
91
105
version = StringUtil .isEmpty (versionStr , false ) ? null : Integer .valueOf (versionStr );
92
- }
106
+ }
93
107
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
+ );
96
113
}
97
114
98
115
if (version == null ) {
99
116
version = 0 ;
100
117
}
101
118
102
119
try {
103
- // 从 Document 查这样的接口
120
+ // 从 Document 查这样的接口
104
121
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 );
106
123
107
- JSONObject result = versionedMap == null ? null : versionedMap .get (version );
124
+ Map < String , Object > result = versionedMap == null ? null : versionedMap .get (version );
108
125
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 ();
110
127
111
128
if (set != null && set .isEmpty () == false ) {
112
- Entry <Integer , JSONObject > maxEntry = null ;
129
+ Entry <Integer , Map < String , Object > > maxEntry = null ;
113
130
114
- for (Entry <Integer , JSONObject > entry : set ) {
131
+ for (Entry <Integer , Map < String , Object > > entry : set ) {
115
132
if (entry == null || entry .getKey () == null || entry .getValue () == null ) {
116
133
continue ;
117
134
}
@@ -144,11 +161,11 @@ public String router(String method, String tag, Map<String, String> params, Stri
144
161
}
145
162
146
163
@ SuppressWarnings ("unchecked" )
147
- APIJSONCreator <T > creator = (APIJSONCreator <T >) APIJSONParser .APIJSON_CREATOR ;
164
+ APIJSONCreator <T , M , L > creator = (APIJSONCreator <T , M , L >) APIJSONParser .APIJSON_CREATOR ;
148
165
if (result == null && Log .DEBUG && APIJSONRouterVerifier .DOCUMENT_MAP .isEmpty ()) {
149
166
150
167
//获取指定的JSON结构 <<<<<<<<<<<<<<
151
- SQLConfig config = creator .createSQLConfig ().setMethod (GET ).setTable (APIJSONConstant .DOCUMENT_ );
168
+ SQLConfig < T , M , L > config = creator .createSQLConfig ().setMethod (GET ).setTable (APIJSONConstant .DOCUMENT_ );
152
169
config .setPrepared (false );
153
170
config .setColumn (Arrays .asList ("request,apijson" ));
154
171
@@ -171,7 +188,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
171
188
// DOCUMENT_MAP.put(cacheKey, versionedMap);
172
189
}
173
190
174
- String apijson = result == null ? null : result . getString ("apijson" );
191
+ String apijson = result == null ? null : getString (result , "apijson" );
175
192
if (StringUtil .isEmpty (apijson , true )) { //
176
193
if (compatCommonAPI ) {
177
194
return crudByTag (method , tag , params , request , session );
@@ -181,37 +198,31 @@ public String router(String method, String tag, Map<String, String> params, Stri
181
198
+ "/" + tag + (versionStr == null ? "" : "?version=" + versionStr ) + " 对应的接口不存在!" );
182
199
}
183
200
184
- JSONObject rawReq = JSON .parseObject (request );
201
+ M rawReq = JSON .parseObject (request );
185
202
if (rawReq == null ) {
186
- rawReq = new JSONObject ( true );
203
+ rawReq = JSON . createJSONObject ( );
187
204
}
188
205
if (params != null && params .isEmpty () == false ) {
189
206
rawReq .putAll (params );
190
207
}
191
208
192
- RequestMethod requestMethod = RequestMethod .valueOf (method .toUpperCase ());
193
- Parser <T > parser = newParser (session , requestMethod );
194
-
195
209
if (parser .isNeedVerifyContent ()) {
196
- Verifier <T > verifier = creator .createVerifier ();
210
+ Verifier <T , M , L > verifier = creator .createVerifier ();
197
211
198
212
//获取指定的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()) {
202
215
throw new UnsupportedOperationException ("找不到 version: " + version + ", method: " + method .toUpperCase () + ", tag: " + tag + " 对应的 structure !"
203
216
+ "非开放请求必须是后端 Request 表中校验规则允许的操作!如果需要则在 Request 表中新增配置!" );
204
217
}
205
218
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 );
210
221
}
211
222
212
- JSONObject apijsonReq = JSON .parseObject (apijson );
223
+ M apijsonReq = JSON .parseObject (apijson );
213
224
if (apijsonReq == null ) {
214
- apijsonReq = new JSONObject ( true );
225
+ apijsonReq = JSON . createJSONObject ( );
215
226
}
216
227
217
228
Set <Entry <String , Object >> rawSet = rawReq .entrySet ();
@@ -225,11 +236,11 @@ public String router(String method, String tag, Map<String, String> params, Stri
225
236
String [] pathKeys = key .split ("\\ ." );
226
237
//逐层到达child的直接容器JSONObject parent
227
238
int last = pathKeys .length - 1 ;
228
- JSONObject parent = apijsonReq ;
239
+ M parent = apijsonReq ;
229
240
for (int i = 0 ; i < last ; i ++) {//一步一步到达指定位置
230
- JSONObject p = parent . getJSONObject (pathKeys [i ]);
241
+ M p = getJSONObject (parent , pathKeys [i ]);
231
242
if (p == null ) {
232
- p = new JSONObject ( true );
243
+ p = JSON . createJSONObject ( );
233
244
parent .put (key , p );
234
245
}
235
246
parent = p ;
@@ -244,7 +255,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
244
255
return parser .setNeedVerifyContent (false ).parse (apijsonReq );
245
256
}
246
257
catch (Exception e ) {
247
- return APIJSONParser . newErrorResult (e ). toJSONString ( );
258
+ return JSON . toJSONString ( parser . newErrorResult (e ));
248
259
}
249
260
}
250
261
0 commit comments