Skip to content

Commit eccf252

Browse files
committed
完善同一个请求内多种不同操作的关键词,新增支持 @post: "User", @gets:{"Privacy": "Privacy-phone" } 等简化写法
1 parent d4be7ce commit eccf252

File tree

4 files changed

+184
-86
lines changed

4 files changed

+184
-86
lines changed

‎APIJSONORM/src/main/java/apijson/JSONObject.java‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
packageapijson;
77

88
importjava.util.ArrayList;
9+
importjava.util.LinkedHashMap;
910
importjava.util.List;
1011
importjava.util.Map;
1112

@@ -151,7 +152,16 @@ public JSONObject setUserIdIn(List<Object> list){
151152
publicstaticfinalStringKEY_ORDER = "@order"; //排序方式
152153
publicstaticfinalStringKEY_RAW = "@raw"; // 自定义原始 SQL 片段
153154
publicstaticfinalStringKEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
154-
publicstaticfinalStringKEY_METHOD = "@method"; //json对象配置操作方法
155+
publicstaticfinalStringKEY_METHOD = "@method"; // json 对象配置操作方法
156+
publicstaticfinalStringKEY_GET = "@get"; // json 对象配置操作方法
157+
publicstaticfinalStringKEY_GETS = "@gets"; // json 对象配置操作方法
158+
publicstaticfinalStringKEY_HEAD = "@head"; // json 对象配置操作方法
159+
publicstaticfinalStringKEY_HEADS = "@heads"; // json 对象配置操作方法
160+
publicstaticfinalStringKEY_POST = "@post"; // json 对象配置操作方法
161+
publicstaticfinalStringKEY_PUT = "@put"; // json 对象配置操作方法
162+
publicstaticfinalStringKEY_DELETE = "@delete"; // json 对象配置操作方法
163+
164+
publicstaticfinalMap<String, RequestMethod> KEY_METHOD_ENUM_MAP;
155165

156166
publicstaticfinalList<String> TABLE_KEY_LIST;
157167
static{
@@ -174,6 +184,22 @@ public JSONObject setUserIdIn(List<Object> list){
174184
TABLE_KEY_LIST.add(KEY_RAW);
175185
TABLE_KEY_LIST.add(KEY_JSON);
176186
TABLE_KEY_LIST.add(KEY_METHOD);
187+
TABLE_KEY_LIST.add(KEY_GET);
188+
TABLE_KEY_LIST.add(KEY_GETS);
189+
TABLE_KEY_LIST.add(KEY_HEAD);
190+
TABLE_KEY_LIST.add(KEY_HEADS);
191+
TABLE_KEY_LIST.add(KEY_POST);
192+
TABLE_KEY_LIST.add(KEY_PUT);
193+
TABLE_KEY_LIST.add(KEY_DELETE);
194+
195+
KEY_METHOD_ENUM_MAP = newLinkedHashMap<>();
196+
KEY_METHOD_ENUM_MAP.put(KEY_GET, RequestMethod.GET);
197+
KEY_METHOD_ENUM_MAP.put(KEY_GETS, RequestMethod.GETS);
198+
KEY_METHOD_ENUM_MAP.put(KEY_HEAD, RequestMethod.HEAD);
199+
KEY_METHOD_ENUM_MAP.put(KEY_HEADS, RequestMethod.HEADS);
200+
KEY_METHOD_ENUM_MAP.put(KEY_POST, RequestMethod.POST);
201+
KEY_METHOD_ENUM_MAP.put(KEY_PUT, RequestMethod.PUT);
202+
KEY_METHOD_ENUM_MAP.put(KEY_DELETE, RequestMethod.DELETE);
177203
}
178204

179205
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>

‎APIJSONORM/src/main/java/apijson/RequestMethod.java‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
packageapijson;
77

8+
importjava.util.Arrays;
9+
importjava.util.List;
10+
811
/**请求方法,对应org.springframework.web.bind.annotation.RequestMethod,多出GETS,HEADS方法
912
* @author Lemon
1013
*/
@@ -41,17 +44,20 @@ public enum RequestMethod{
4144
PUT,
4245

4346
/**
44-
* json包含多条语句,支持增删改查,函数调用
47+
* 删除数据
4548
*/
46-
CRUD,
47-
49+
DELETE,
50+
4851
/**
49-
* 删除数据
52+
* json 包含多条语句,支持增删改查、函数调用
5053
*/
51-
DELETE;
52-
53-
publicstaticfinalRequestMethod[] ALL = newRequestMethod[]{GET, HEAD, GETS, HEADS, POST, PUT, CRUD, DELETE};
54+
CRUD;
5455

56+
publicstaticfinalRequestMethod[] ALL = newRequestMethod[]{GET, HEAD, GETS, HEADS, POST, PUT, DELETE, CRUD };
57+
publicstaticfinalList<String> ALL_NAME_LIST = Arrays.asList(
58+
GET.name(), HEAD.name(), GETS.name(), HEADS.name(), POST.name(), PUT.name(), DELETE.name(), CRUD.name()
59+
);
60+
5561
/**是否为GET请求方法
5662
* @param method
5763
* @param containPrivate 包含私密(非明文)获取方法GETS

‎APIJSONORM/src/main/java/apijson/orm/AbstractParser.java‎

Lines changed: 86 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ public JSONObject parseResponse(JSONObject request){
410410
requestObject = request;
411411
try{
412412
setVersion(requestObject.getIntValue(JSONRequest.KEY_VERSION));
413+
requestObject.remove(JSONRequest.KEY_VERSION);
414+
413415
if (getMethod() != RequestMethod.CRUD){
414416
setTag(requestObject.getString(JSONRequest.KEY_TAG));
415417
requestObject.remove(JSONRequest.KEY_TAG);
416418
}
417-
requestObject.remove(JSONRequest.KEY_VERSION);
418419
} catch (Exceptione){
419420
returnextendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
420421
}
@@ -2089,7 +2090,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
20892090
}
20902091

20912092
protectedJSONObjectbatchVerify(RequestMethodmethod, Stringtag, intversion, Stringname, @NotNullJSONObjectrequest, intmaxUpdateCount, SQLCreatorcreator) throwsException{
2092-
JSONObjectjsonObject = newJSONObject(true);
2093+
JSONObjectcorrectRequest = newJSONObject(true);
20932094
List<String> removeTmpKeys = newArrayList<>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
20942095

20952096
Set<String> reqSet = request == null ? null : request.keySet();
@@ -2098,49 +2099,82 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
20982099
}
20992100

21002101
for (Stringkey : reqSet){
2101-
// key重复直接抛错(xxx:alias, xxx:alias[])
2102-
if (jsonObject.containsKey(key) || jsonObject.containsKey(key + apijson.JSONObject.KEY_ARRAY)){
2103-
thrownewIllegalArgumentException("对象名重复,请添加别名区分 ! ,重复对象名为: " + key);
2102+
// key 重复直接抛错(xxx:alias, xxx:alias[])
2103+
if (correctRequest.containsKey(key) || correctRequest.containsKey(key + apijson.JSONObject.KEY_ARRAY)){
2104+
thrownewIllegalArgumentException("对象名重复,请添加别名区分 ! 重复对象名为: " + key);
21042105
}
21052106

2106-
// @post、@get等RequestMethod
2107+
// @post、@get 等 RequestMethod
21072108
try{
2108-
if (key.startsWith("@") && getEnum(RequestMethod.class, key.substring(1).toUpperCase(), null) != null){
2109+
RequestMethodkeyMethod = apijson.orm.JSONRequest.KEY_METHOD_ENUM_MAP.get(key);
2110+
if (keyMethod != null){
21092111
// 如果不匹配,异常不处理即可
2110-
RequestMethod_method = RequestMethod.valueOf(key.substring(1).toUpperCase());
21112112
removeTmpKeys.add(key);
21122113

2113-
JSONObjectobj = request.getJSONObject(key);
2114-
Set<String> set = obj == null ? newHashSet<>() : obj.keySet();
2114+
Objectval = request.get(key);
2115+
JSONObjectobj = valinstanceofJSONObject ? request.getJSONObject(key) : null;
2116+
if (obj == null){
2117+
if (valinstanceofString){
2118+
String[] tbls = StringUtil.split((String) val);
2119+
if (tbls != null && tbls.length > 0){
2120+
obj = newJSONObject(true);
2121+
for (inti = 0; i < tbls.length; i++){
2122+
Stringtbl = tbls[i];
2123+
if (obj.containsKey(tbl)){
2124+
thrownewConflictException(key + ": value 中 " + tbl + " 已经存在,不能重复!");
2125+
}
2126+
obj.put(tbl, newJSONObject(true));
2127+
}
2128+
}
2129+
}
2130+
else{
2131+
thrownewIllegalArgumentException(key + ": value 中 value 类型错误,只能是 String 或 JSONObject{} !");
2132+
}
2133+
}
2134+
2135+
Set<Entry<String, Object>> set = obj == null ? newHashSet<>() : obj.entrySet();
21152136

2116-
for (StringobjKey : set){
2137+
for (Entry<String, Object> objEntry : set){
2138+
StringobjKey = objEntry == null ? null : objEntry.getKey();
21172139
if (objKey == null){
21182140
continue;
21192141
}
21202142

21212143
Map<String, Object> objAttrMap = newHashMap<>();
2122-
objAttrMap.put(apijson.JSONObject.KEY_METHOD, _method);
2144+
objAttrMap.put(apijson.JSONObject.KEY_METHOD, keyMethod);
21232145
keyObjectAttributesMap.put(objKey, objAttrMap);
2124-
JSONObjectobjAttrJson = obj.getJSONObject(objKey);
2125-
Set<Entry<String, Object>> objSet = objAttrJson == null ? newHashSet<>() : objAttrJson.entrySet();
21262146

2127-
for (Entry<String, Object> entry : objSet){
2128-
StringobjAttrKey = entry == null ? null : entry.getKey();
2129-
if (objAttrKey == null){
2130-
continue;
2147+
ObjectobjVal = objEntry.getValue();
2148+
JSONObjectobjAttrJson = objValinstanceofJSONObject ? obj.getJSONObject(objKey) : null;
2149+
if (objAttrJson == null){
2150+
if (objValinstanceofString){
2151+
objAttrMap.put(JSONRequest.KEY_TAG, objVal);
21312152
}
2153+
else{
2154+
thrownewIllegalArgumentException(key + ":{" + objKey + ": value 中 value 类型错误,只能是 String 或 JSONObject{} !");
2155+
}
2156+
}
2157+
else{
2158+
Set<Entry<String, Object>> objSet = objAttrJson == null ? newHashSet<>() : objAttrJson.entrySet();
21322159

2133-
switch (objAttrKey){
2134-
caseapijson.JSONObject.KEY_DATASOURCE:
2135-
caseapijson.JSONObject.KEY_SCHEMA:
2136-
caseapijson.JSONObject.KEY_DATABASE:
2137-
caseJSONRequest.KEY_VERSION:
2138-
caseapijson.JSONObject.KEY_ROLE:
2139-
caseJSONRequest.KEY_TAG:
2140-
objAttrMap.put(objAttrKey, entry.getValue());
2141-
break;
2142-
default:
2143-
break;
2160+
for (Entry<String, Object> entry : objSet){
2161+
StringobjAttrKey = entry == null ? null : entry.getKey();
2162+
if (objAttrKey == null){
2163+
continue;
2164+
}
2165+
2166+
switch (objAttrKey){
2167+
caseapijson.JSONObject.KEY_DATASOURCE:
2168+
caseapijson.JSONObject.KEY_SCHEMA:
2169+
caseapijson.JSONObject.KEY_DATABASE:
2170+
caseJSONRequest.KEY_VERSION:
2171+
caseapijson.JSONObject.KEY_ROLE:
2172+
caseJSONRequest.KEY_TAG:
2173+
objAttrMap.put(objAttrKey, entry.getValue());
2174+
break;
2175+
default:
2176+
break;
2177+
}
21442178
}
21452179
}
21462180
}
@@ -2189,15 +2223,17 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
21892223
}
21902224

21912225
if (key.startsWith("@") || key.endsWith("@")){
2192-
jsonObject.put(key, obj);
2226+
correctRequest.put(key, obj);
21932227
continue;
21942228
}
21952229

21962230
if (objinstanceofJSONObject || objinstanceofJSONArray){
2197-
RequestMethod_method = null;
2231+
RequestMethod_method;
21982232
if (objinstanceofJSONObject){
2199-
_method = RequestMethod.valueOf(request.getJSONObject(key).getString(apijson.JSONObject.KEY_METHOD).toUpperCase());
2200-
Stringcombine = request.getJSONObject(key).getString(KEY_COMBINE);
2233+
JSONObjecttblObj = request.getJSONObject(key);
2234+
Stringmn = tblObj == null ? null : tblObj.getString(apijson.JSONObject.KEY_METHOD);
2235+
_method = mn == null ? null : RequestMethod.valueOf(mn);
2236+
Stringcombine = _method == null ? null : tblObj.getString(KEY_COMBINE);
22012237
if (combine != null && RequestMethod.isPublicMethod(_method) == false){
22022238
thrownewIllegalArgumentException(key + ":{} 里的 @combine:value 不合法!开放请求 GET、HEAD 才允许传 @combine:value !");
22032239
}
@@ -2207,22 +2243,14 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22072243
if (attrMap == null){
22082244
if (method == RequestMethod.CRUD){
22092245
_method = GET;
2210-
if (attrMap == null){
2211-
Map<String, Object> objAttrMap = newHashMap<>();
2212-
objAttrMap.put(apijson.JSONObject.KEY_METHOD, GET);
2213-
keyObjectAttributesMap.put(key, objAttrMap);
2214-
} else{
2215-
attrMap.put(apijson.JSONObject.KEY_METHOD, GET);
2216-
}
2246+
Map<String, Object> objAttrMap = newHashMap<>();
2247+
objAttrMap.put(apijson.JSONObject.KEY_METHOD, GET);
2248+
keyObjectAttributesMap.put(key, objAttrMap);
22172249
} else{
22182250
_method = method;
2219-
if (attrMap == null){
2220-
Map<String, Object> objAttrMap = newHashMap<>();
2221-
objAttrMap.put(apijson.JSONObject.KEY_METHOD, method);
2222-
keyObjectAttributesMap.put(key, objAttrMap);
2223-
} else{
2224-
attrMap.put(apijson.JSONObject.KEY_METHOD, method);
2225-
}
2251+
Map<String, Object> objAttrMap = newHashMap<>();
2252+
objAttrMap.put(apijson.JSONObject.KEY_METHOD, method);
2253+
keyObjectAttributesMap.put(key, objAttrMap);
22262254
}
22272255
} else{
22282256
_method = (RequestMethod) attrMap.get(apijson.JSONObject.KEY_METHOD);
@@ -2236,42 +2264,42 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22362264

22372265
// get请求不校验
22382266
if (RequestMethod.isPublicMethod(_method)){
2239-
jsonObject.put(key, obj);
2267+
correctRequest.put(key, obj);
22402268
continue;
22412269
}
22422270

2243-
if(tag != null && !tag.contains(":")){
2271+
if(tag != null && !tag.contains(":")){
22442272
JSONObjectobject = getRequestStructure(_method, tag, version);
22452273
JSONObjectret = objectVerify(_method, tag, version, name, request, maxUpdateCount, creator, object);
2246-
jsonObject.putAll(ret);
2274+
correctRequest.putAll(ret);
22472275
break;
22482276
}
22492277

22502278
String_tag = buildTag(request, key, method, tag);
22512279
JSONObjectobject = getRequestStructure(_method, _tag, version);
2252-
if(method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)){
2280+
if(method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)){
22532281
JSONObjectrequestItem = newJSONObject();
22542282
requestItem.put(key, obj);
22552283
JSONObjectret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
2256-
jsonObject.put(key, ret.get(key));
2284+
correctRequest.put(key, ret.get(key));
22572285
} else{
22582286
returnobjectVerify(_method, _tag, version, name, request, maxUpdateCount, creator, object);
22592287
}
22602288
} else{
2261-
jsonObject.put(key, obj);
2289+
correctRequest.put(key, obj);
22622290
}
22632291
} catch (Exceptione){
22642292
e.printStackTrace();
22652293
thrownewException(e);
22662294
}
22672295
}
22682296

2269-
// 这里是requestObject ref request 的引用, 删除不需要的临时变量
2297+
// 这里是 requestObject ref request 的引用, 删除不需要的临时变量
22702298
for (StringremoveKey : removeTmpKeys){
22712299
request.remove(removeKey);
22722300
}
22732301

2274-
returnjsonObject;
2302+
returncorrectRequest;
22752303
}
22762304

22772305
publicstatic <EextendsEnum<E>> EgetEnum(finalClass<E> enumClass, finalStringenumName, finalEdefaultEnum){
@@ -2284,7 +2312,7 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
22842312
returndefaultEnum;
22852313
}
22862314
}
2287-
2315+
22882316
protectedvoidsetRequestAttribute(Stringkey, booleanisArray, StringattrKey, @NotNullJSONObjectrequest){
22892317
Map<String, Object> attrMap = keyObjectAttributesMap.get(isArray ? key + apijson.JSONObject.KEY_ARRAY : key);
22902318
ObjectattrVal = attrMap == null ? null : attrMap.get(attrKey);
@@ -2308,7 +2336,7 @@ protected String buildTag(JSONObject request, String key, RequestMethod method,
23082336
}
23092337
returntag;
23102338
}
2311-
2339+
23122340

23132341
protectedJSONObjectobjectVerify(RequestMethodmethod, Stringtag, intversion, Stringname, @NotNullJSONObjectrequest
23142342
, intmaxUpdateCount, SQLCreatorcreator, JSONObjectobject) throwsException{
@@ -2317,7 +2345,7 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
23172345
// JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是{}
23182346
returngetVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobalDatabase(), getGlobalSchema(), creator);
23192347
}
2320-
2348+
23212349
/***
23222350
* 兼容url crud, 获取真实method
23232351
* @param method = crud

0 commit comments

Comments
(0)