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
package apijson;
77

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

@@ -151,7 +152,16 @@ public JSONObject setUserIdIn(List<Object> list) {
151152
public static final String KEY_ORDER = "@order"; //排序方式
152153
public static final String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
153154
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
154-
public static final String KEY_METHOD = "@method"; //json对象配置操作方法
155+
public static final String KEY_METHOD = "@method"; // json 对象配置操作方法
156+
public static final String KEY_GET = "@get"; // json 对象配置操作方法
157+
public static final String KEY_GETS = "@gets"; // json 对象配置操作方法
158+
public static final String KEY_HEAD = "@head"; // json 对象配置操作方法
159+
public static final String KEY_HEADS = "@heads"; // json 对象配置操作方法
160+
public static final String KEY_POST = "@post"; // json 对象配置操作方法
161+
public static final String KEY_PUT = "@put"; // json 对象配置操作方法
162+
public static final String KEY_DELETE = "@delete"; // json 对象配置操作方法
163+
164+
public static final Map<String, RequestMethod> KEY_METHOD_ENUM_MAP;
155165

156166
public static final List<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 = new LinkedHashMap<>();
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
package apijson;
77

8+
import java.util.Arrays;
9+
import java.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-
public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, CRUD, DELETE};
54+
CRUD;
5455

56+
public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, DELETE, CRUD };
57+
public static final List<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 (Exception e) {
419420
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
420421
}
@@ -2089,7 +2090,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
20892090
}
20902091

20912092
protected JSONObject batchVerify(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request, int maxUpdateCount, SQLCreator creator) throws Exception {
2092-
JSONObject jsonObject = new JSONObject(true);
2093+
JSONObject correctRequest = new JSONObject(true);
20932094
List<String> removeTmpKeys = new ArrayList<>(); // 请求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 (String key : reqSet) {
2101-
// key重复直接抛错(xxx:alias, xxx:alias[])
2102-
if (jsonObject.containsKey(key) || jsonObject.containsKey(key + apijson.JSONObject.KEY_ARRAY)) {
2103-
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! ,重复对象名为: " + key);
2102+
// key 重复直接抛错(xxx:alias, xxx:alias[])
2103+
if (correctRequest.containsKey(key) || correctRequest.containsKey(key + apijson.JSONObject.KEY_ARRAY)) {
2104+
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! 重复对象名为: " + 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+
RequestMethod keyMethod = 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-
JSONObject obj = request.getJSONObject(key);
2114-
Set<String> set = obj == null ? new HashSet<>() : obj.keySet();
2114+
Object val = request.get(key);
2115+
JSONObject obj = val instanceof JSONObject ? request.getJSONObject(key) : null;
2116+
if (obj == null) {
2117+
if (val instanceof String) {
2118+
String[] tbls = StringUtil.split((String) val);
2119+
if (tbls != null && tbls.length > 0) {
2120+
obj = new JSONObject(true);
2121+
for (int i = 0; i < tbls.length; i++) {
2122+
String tbl = tbls[i];
2123+
if (obj.containsKey(tbl)) {
2124+
throw new ConflictException(key + ": value 中 " + tbl + " 已经存在,不能重复!");
2125+
}
2126+
obj.put(tbl, new JSONObject(true));
2127+
}
2128+
}
2129+
}
2130+
else {
2131+
throw new IllegalArgumentException(key + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !");
2132+
}
2133+
}
2134+
2135+
Set<Entry<String, Object>> set = obj == null ? new HashSet<>() : obj.entrySet();
21152136

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

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

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

2133-
switch (objAttrKey) {
2134-
case apijson.JSONObject.KEY_DATASOURCE:
2135-
case apijson.JSONObject.KEY_SCHEMA:
2136-
case apijson.JSONObject.KEY_DATABASE:
2137-
case JSONRequest.KEY_VERSION:
2138-
case apijson.JSONObject.KEY_ROLE:
2139-
case JSONRequest.KEY_TAG:
2140-
objAttrMap.put(objAttrKey, entry.getValue());
2141-
break;
2142-
default:
2143-
break;
2160+
for (Entry<String, Object> entry : objSet) {
2161+
String objAttrKey = entry == null ? null : entry.getKey();
2162+
if (objAttrKey == null) {
2163+
continue;
2164+
}
2165+
2166+
switch (objAttrKey) {
2167+
case apijson.JSONObject.KEY_DATASOURCE:
2168+
case apijson.JSONObject.KEY_SCHEMA:
2169+
case apijson.JSONObject.KEY_DATABASE:
2170+
case JSONRequest.KEY_VERSION:
2171+
case apijson.JSONObject.KEY_ROLE:
2172+
case JSONRequest.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 (obj instanceof JSONObject || obj instanceof JSONArray) {
2197-
RequestMethod _method = null;
2231+
RequestMethod _method;
21982232
if (obj instanceof JSONObject) {
2199-
_method = RequestMethod.valueOf(request.getJSONObject(key).getString(apijson.JSONObject.KEY_METHOD).toUpperCase());
2200-
String combine = request.getJSONObject(key).getString(KEY_COMBINE);
2233+
JSONObject tblObj = request.getJSONObject(key);
2234+
String mn = tblObj == null ? null : tblObj.getString(apijson.JSONObject.KEY_METHOD);
2235+
_method = mn == null ? null : RequestMethod.valueOf(mn);
2236+
String combine = _method == null ? null : tblObj.getString(KEY_COMBINE);
22012237
if (combine != null && RequestMethod.isPublicMethod(_method) == false) {
22022238
throw new IllegalArgumentException(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 = new HashMap<>();
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 = new HashMap<>();
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 = new HashMap<>();
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 = new HashMap<>();
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
JSONObject object = getRequestStructure(_method, tag, version);
22452273
JSONObject ret = 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
JSONObject object = getRequestStructure(_method, _tag, version);
2252-
if(method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
2280+
if (method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
22532281
JSONObject requestItem = new JSONObject();
22542282
requestItem.put(key, obj);
22552283
JSONObject ret = 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
return objectVerify(_method, _tag, version, name, request, maxUpdateCount, creator, object);
22592287
}
22602288
} else {
2261-
jsonObject.put(key, obj);
2289+
correctRequest.put(key, obj);
22622290
}
22632291
} catch (Exception e) {
22642292
e.printStackTrace();
22652293
throw new Exception(e);
22662294
}
22672295
}
22682296

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

2274-
return jsonObject;
2302+
return correctRequest;
22752303
}
22762304

22772305
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, final E defaultEnum) {
@@ -2284,7 +2312,7 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
22842312
return defaultEnum;
22852313
}
22862314
}
2287-
2315+
22882316
protected void setRequestAttribute(String key, boolean isArray, String attrKey, @NotNull JSONObject request) {
22892317
Map<String, Object> attrMap = keyObjectAttributesMap.get(isArray ? key + apijson.JSONObject.KEY_ARRAY : key);
22902318
Object attrVal = attrMap == null ? null : attrMap.get(attrKey);
@@ -2308,7 +2336,7 @@ protected String buildTag(JSONObject request, String key, RequestMethod method,
23082336
}
23092337
return tag;
23102338
}
2311-
2339+
23122340

23132341
protected JSONObject objectVerify(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request
23142342
, int maxUpdateCount, SQLCreator creator, JSONObject object) throws Exception {
@@ -2317,7 +2345,7 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
23172345
// JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
23182346
return getVerifier().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)