Skip to content

Commit 9b7815d

Browse files
committed
更新table;服务端非GET请求返回JSON增加id;删除RequestParser多余代码
1 parent 9553367 commit 9b7815d

File tree

12 files changed

+166
-213
lines changed

12 files changed

+166
-213
lines changed

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/RequestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static JSONObject newPutRequest() {
5656
list.add("http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000");
5757
list.add("http://common.cnblogs.com/images/icon_weibo_24.png");
5858
list.add("http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000");
59-
data.setPicture(JSON.toJSONString(list));
59+
data.setPicture(JSON.toJSONString(list));//"\"" + JSON.toJSONString(new JSONObject("{\"id\":2}")) + "\"");//
6060

6161
return new JSONRequest(data).setTag(User.class.getSimpleName());
6262
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package zuo.biao.apijson;
2+
3+
public class Table {
4+
5+
public static final String ID = "id";
6+
public static final String NAME = "name";
7+
public static final String SEX = "sex";
8+
public static final String PHONE = "phone";
9+
public static final String HEAD = "head";
10+
public static final String PICTURE = "picture";
11+
public static final String PASSWORD = "password";
12+
public static final String TITLE = "title";
13+
public static final String CONTENT = "content";
14+
15+
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import zuo.biao.apijson.JSON;
2727
import zuo.biao.apijson.StringUtil;
28+
import zuo.biao.apijson.Table;
2829

2930
/**config model for query
3031
* @author Lemon
@@ -33,6 +34,7 @@ public class QueryConfig {
3334

3435
public static final String KEY_COLUMNS = "columns";
3536

37+
private long id;
3638
private RequestMethod method;
3739
private String table;
3840
private String columns;
@@ -102,6 +104,14 @@ private String getColumnsString() {
102104
}
103105
}
104106

107+
public long getId() {
108+
return id;
109+
}
110+
public QueryConfig setId(long id) {
111+
this.id = id;
112+
return this;
113+
}
114+
105115
public String getValues() {
106116
return values;
107117
}
@@ -209,13 +219,13 @@ public String getSetString() {
209219
public static String getSetString(Map<String, Object> where) {
210220
Set<String> set = where == null ? null : where.keySet();
211221
if (set != null && set.size() > 0) {
212-
if (where.containsKey("id") == false) {
222+
if (where.containsKey(Table.ID) == false) {
213223
return "";
214224
}
215225
String setString = " set ";
216226
for (String key : set) {
217227
//避免筛选到全部 value = key == null ? null : where.get(key);
218-
if (key == null || "id".equals(key)) {
228+
if (key == null || Table.ID.equals(key)) {
219229
continue;
220230
}
221231
setString += (key + "='" + where.get(key) + "' ,");
@@ -224,7 +234,7 @@ public static String getSetString(Map<String, Object> where) {
224234
setString = setString.substring(0, setString.length() - 1);
225235
}
226236
if (setString.trim().endsWith("set") == false) {
227-
return setString + " where id='" + where.get("id") + "' ";
237+
return setString + " where " + Table.ID + "='" + where.get(Table.ID) + "' ";
228238
}
229239
}
230240
return "";
@@ -271,7 +281,7 @@ public static synchronized QueryConfig getQueryConfig(RequestMethod method, Stri
271281
}
272282
}
273283

274-
return config;
284+
return config.setId(request.getLongValue(Table.ID));
275285
}
276286

277287
/**

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/RequestParser.java

Lines changed: 5 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.URLDecoder;
2020
import java.rmi.AccessException;
2121
import java.util.ArrayList;
22-
import java.util.Arrays;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
@@ -31,6 +30,7 @@
3130

3231
import zuo.biao.apijson.JSON;
3332
import zuo.biao.apijson.StringUtil;
33+
import zuo.biao.apijson.Table;
3434
import zuo.biao.apijson.server.sql.AccessVerifier;
3535
import zuo.biao.apijson.server.sql.QueryHelper;
3636

@@ -156,106 +156,6 @@ private JSONObject fillTarget(JSONObject target, final JSONObject request, Strin
156156
return null;
157157
}
158158

159-
// //TODO 方法一 :逻辑错了,不是填充target里的key,而是把request对应的所有key-value填充至target
160-
// Set<String> set = target.keySet();
161-
// if (set != null) {
162-
// String value;
163-
// JSONObject child;
164-
// for (String key : set) {
165-
// value = target.getString(key);
166-
// if (DISALLOW_COLUMNS.equals(key)) {
167-
// if (isContainKeyInArray(request, StringUtil.split(value))) {
168-
// throw new IllegalArgumentException("不允许传[" + value + "]内的任何字段!");
169-
// }
170-
// } else if (NECESSARY_COLUMNS.equals(key)) {
171-
// if (isContainAllKeyInArray(request, StringUtil.split(value)) == false) {
172-
// throw new IllegalArgumentException(requestName + "缺少[" + value + "]内的某些字段!");
173-
// }
174-
// }
175-
//
176-
// child = JSON.parseObject(value);
177-
// if (child == null) {//key - value
178-
// target.put(key, request.getString(key));//提取key-value
179-
// } else { //object
180-
// target.put(key, filterTarget(child, request.getJSONObject(key), key));//往下一级提取
181-
// }
182-
// }
183-
// }
184-
185-
186-
187-
188-
// // 方法二:行不通,target外层可能加上tag,也可能不加
189-
// String necessarys = StringUtil.getNoBlankString(target.getString(NECESSARY_COLUMNS));
190-
// String disallows = StringUtil.getNoBlankString(target.getString(DISALLOW_COLUMNS));
191-
//
192-
//
193-
// //提取必须的字段 <<<<<<<<<<<<<<<<<<<
194-
// String[] necessaryColumns = StringUtil.split(necessarys);
195-
// String value;
196-
// if (necessaryColumns != null) {
197-
// for (String column : necessaryColumns) {
198-
// if (StringUtil.isNotEmpty(column, true) == false) {
199-
// continue;
200-
// }
201-
// value = request.getString(column);
202-
// if (StringUtil.isNotEmpty(value, true) == false) {
203-
// throw new IllegalArgumentException(requestName + "缺少[" + necessarys + "]内的字段 " + column + " !");
204-
// }
205-
// target.put(column, value);//提取key-value
206-
// request.remove(column);
207-
// }
208-
// target.put(NECESSARY_COLUMNS, necessarys);
209-
// target.put(DISALLOW_COLUMNS, disallows);
210-
// }
211-
// //提取提取必须的字段 >>>>>>>>>>>>>>>>>>>
212-
//
213-
// //排除不允许的字段,提取可选字段 <<<<<<<<<<<<<<<<<<<
214-
// String[] disallowColumns = null;
215-
// Set<String> set = request.keySet();
216-
// if ("!".equals(disallows)) {//所有非necessaryColumns
217-
// if (set != null && set.isEmpty() == false) {
218-
// throw new IllegalArgumentException("不允许传[" + disallows + "]内的任何字段!");
219-
// }
220-
// } else {
221-
// disallowColumns = StringUtil.split(disallows);
222-
// }
223-
// if (set != null) {
224-
// for (String column : set) {
225-
// if (StringUtil.isNotEmpty(column, true) == false) {
226-
// continue;
227-
// }
228-
// if (isContainInArray(column, disallowColumns)) {
229-
// throw new IllegalArgumentException("不允许传[" + disallows + "]内的任何字段!");
230-
// }
231-
// target.put(column, request.getString(column));//提取可选字段
232-
//// request.remove(column);
233-
// }
234-
//
235-
// target.put(NECESSARY_COLUMNS, necessarys);
236-
// target.put(DISALLOW_COLUMNS, disallows);
237-
// }
238-
// //排除不允许的字段,提取可选字段 >>>>>>>>>>>>>>>>>>>
239-
//
240-
//
241-
// set = target.keySet();
242-
// if (set != null) {
243-
// JSONObject child;
244-
// for (String key : set) {
245-
// child = JSON.parseObject(target.getString(key));
246-
// if (child != null) { //object
247-
// target.put(key, fillTarget(child, request.getJSONObject(key), key));//往下一级提取
248-
// }
249-
// }
250-
// }
251-
//
252-
// target.remove(DISALLOW_COLUMNS);
253-
// target.remove(NECESSARY_COLUMNS);
254-
255-
256-
257-
258-
259159
// 方法三:遍历request,transferredRequest只添加target所包含的object,且移除target中DISALLOW_COLUMNS,期间判断NECESSARY_COLUMNS是否都有
260160
String necessarys = StringUtil.getNoBlankString(target.getString(NECESSARY_COLUMNS));
261161
String[] necessaryColumns = StringUtil.split(necessarys);
@@ -311,13 +211,16 @@ private JSONObject fillTarget(JSONObject target, final JSONObject request, Strin
311211
throw new IllegalArgumentException(requestName
312212
+ "不能缺少 " + key + ":{} 等[" + necessarys + "]内的任何JSONObject!");
313213
}
214+
if (requestMethod == RequestMethod.POST && result.containsKey(Table.ID) == false) {//为注册用户返回id
215+
result.put(Table.ID, System.currentTimeMillis());
216+
}
314217
transferredRequest.put(key, result);
315218
}
316219
}
317220
}
318221
}
319222

320-
System.out.println(TAG + "filterTarget return target = " + JSON.toJSONString(target));
223+
System.out.println(TAG + "filterTarget return transferredRequest = " + JSON.toJSONString(transferredRequest));
321224
return transferredRequest;
322225
}
323226

@@ -345,92 +248,6 @@ private boolean isContainInArray(String key, String[] array) {
345248
return false;
346249
}
347250

348-
/**array至少有一个值在request的key中
349-
* @param request
350-
* @param array
351-
* @return
352-
*/
353-
private boolean isContainKeyInArray(JSONObject request, String[] array) {
354-
if (array == null || array.length <= 0) {
355-
System.out.println(TAG + "isContainKeyInArray array == null || array.length <= 0 >> return false;");
356-
return false;
357-
}
358-
if (request == null) {
359-
System.out.println(TAG + "isContainKeyInArray request == null >> return false;");
360-
return false;
361-
}
362-
363-
for (String s : array) {
364-
if (request.containsKey(s) == true) {
365-
return true;
366-
}
367-
}
368-
369-
return false;
370-
}
371-
/**array的所有值都在request的key中
372-
* @param request
373-
* @param array
374-
* @return
375-
*/
376-
private boolean isContainAllKeyInArray(JSONObject request, String[] array) {
377-
if (array == null || array.length <= 0) {
378-
System.out.println(TAG + "isContainAllKeyInArray array == null || array.length <= 0 >> return true;");
379-
return true;
380-
}
381-
if (request == null) {
382-
System.out.println(TAG + "isContainAllKeyInArray request == null >> return false;");
383-
return false;
384-
}
385-
386-
for (String s : array) {
387-
if (request.containsKey(s) == false) {
388-
return false;
389-
}
390-
}
391-
392-
return true;
393-
}
394-
// /**array至少有一个值在request的key中
395-
// * @param request
396-
// * @param array
397-
// * @return
398-
// */
399-
// private boolean isContainKeyInArray(JSONObject request, String[] array) {
400-
// return verifyContainKeyInArray(request, array, true);
401-
// }
402-
// /**array的所有值都在request的key中
403-
// * @param request
404-
// * @param array
405-
// * @return
406-
// */
407-
// private boolean isContainAllKeyInArray(JSONObject request, String[] array) {
408-
// return verifyContainKeyInArray(request, array, false);
409-
// }
410-
/**判断array的值和request的key的相同性
411-
* @param request
412-
* @param array
413-
* @param contain
414-
* @return
415-
*/
416-
private boolean verifyContainKeyInArray(JSONObject request, String[] array, boolean contain) {
417-
if (array == null || array.length <= 0) {
418-
System.out.println(TAG + "verifyContainKeyInArray array == null || array.length <= 0 >> return ! contain;");
419-
return ! contain;
420-
}
421-
if (request == null) {
422-
System.out.println(TAG + "verifyContainKeyInArray request == null >> return false;");
423-
return false;
424-
}
425-
426-
for (String s : array) {
427-
if (request.containsKey(s) == contain) {
428-
return contain;
429-
}
430-
}
431-
432-
return ! contain;
433-
}
434251

435252
/**获取单个对象,该对象处于parentObject内
436253
* @param parentPath parentObject的路径

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/sql/QueryHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.alibaba.fastjson.JSONObject;
2222
import zuo.biao.apijson.StringUtil;
23+
import zuo.biao.apijson.Table;
2324
import zuo.biao.apijson.server.QueryConfig;
2425

2526
/**helper for query MySQL database
@@ -101,7 +102,10 @@ public JSONObject select(QueryConfig config) throws SQLException, Exception {
101102
case PUT:
102103
case DELETE:
103104
int updateCount = statement.executeUpdate(sql);//创建数据对象
104-
return newJSONObject(updateCount > 0 ? 200 : 1149, updateCount > 0 ? "success" : "failed,可能对象不存在!");
105+
106+
JSONObject result = newJSONObject(updateCount > 0 ? 200 : 1149, updateCount > 0 ? "success" : "failed,可能对象不存在!");
107+
result.put(Table.ID, config.getId());
108+
return result;
105109
default:
106110
rs = statement.executeQuery(sql);//创建数据对象
107111
break;

table/sys_Access.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- MySQL dump 10.13 Distrib 5.7.12, for osx10.9 (x86_64)
2+
--
3+
-- Host: localhost Database: sys
4+
-- ------------------------------------------------------
5+
-- Server version 5.7.16
6+
7+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10+
/*!40101 SET NAMES utf8 */;
11+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12+
/*!40103 SET TIME_ZONE='+00:00' */;
13+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17+
18+
--
19+
-- Table structure for table `Access`
20+
--
21+
22+
DROP TABLE IF EXISTS `Access`;
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `Access` (
26+
`id` int(11) NOT NULL AUTO_INCREMENT,
27+
`table` varchar(10) NOT NULL,
28+
`method` varchar(10) DEFAULT 'GET',
29+
`password` varchar(100) DEFAULT NULL,
30+
PRIMARY KEY (`id`)
31+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
32+
/*!40101 SET character_set_client = @saved_cs_client */;
33+
34+
--
35+
-- Dumping data for table `Access`
36+
--
37+
38+
LOCK TABLES `Access` WRITE;
39+
/*!40000 ALTER TABLE `Access` DISABLE KEYS */;
40+
INSERT INTO `Access` VALUES (1,'Wallet','GET','loginPassword, payPassword'),(2,'Wallet','DELETE','loginPassword, payPassword'),(3,'Work','DELETE','loginPassword'),(4,'User','PUT','loginPassword'),(5,'User','DELETE','loginPassword'),(6,'Comment','DELETE','loginPassword');
41+
/*!40000 ALTER TABLE `Access` ENABLE KEYS */;
42+
UNLOCK TABLES;
43+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
44+
45+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
46+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
47+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
48+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
49+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
50+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
51+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
52+
53+
-- Dump completed on 2017-01-24 16:19:12

0 commit comments

Comments
 (0)