Skip to content

Commit eb051b0

Browse files
committed
Java:APIJSONDemo 升级 APIJSON 8 和 MySQL-JDBC 9.2, PostgreSQL-JDBC 42.7.2
1 parent fdcc49c commit eb051b0

File tree

9 files changed

+344
-23
lines changed

9 files changed

+344
-23
lines changed

APIJSON-Java-Server/APIJSONDemo/pom.xml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@
2020
</properties>
2121

2222
<dependencies>
23+
24+
<dependency>
25+
<groupId>com.alibaba</groupId>
26+
<artifactId>fastjson</artifactId>
27+
<version>1.2.83</version>
28+
</dependency>
29+
2330
<!-- 需要的 APIJSON 相关依赖 -->
2431
<dependency>
2532
<groupId>com.github.Tencent</groupId>
2633
<artifactId>APIJSON</artifactId>
27-
<version>7.1.0</version>
34+
<version>8.0.0.0.0</version>
2835
</dependency>
2936
<dependency>
3037
<groupId>com.github.APIJSON</groupId>
3138
<artifactId>apijson-framework</artifactId>
32-
<version>7.1.5</version>
39+
<version>7.2.0.0</version>
3340
</dependency>
3441
<dependency>
3542
<groupId>jakarta.servlet</groupId>
@@ -39,14 +46,14 @@
3946

4047
<!-- 需要用的数据库 JDBC 驱动 -->
4148
<dependency>
42-
<groupId>mysql</groupId>
43-
<artifactId>mysql-connector-java</artifactId>
44-
<version>8.0.29</version>
49+
<groupId>com.mysql</groupId>
50+
<artifactId>mysql-connector-j</artifactId>
51+
<version>9.2.0</version>
4552
</dependency>
4653
<dependency>
4754
<groupId>org.postgresql</groupId>
4855
<artifactId>postgresql</artifactId>
49-
<version>42.3.8</version>
56+
<version>42.7.2</version>
5057
</dependency>
5158
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
5259

@@ -66,6 +73,12 @@
6673
<!-- </exclusion>-->
6774
</exclusions>
6875
</dependency>
76+
<dependency>
77+
<groupId>com.google.protobuf</groupId>
78+
<artifactId>protobuf-java</artifactId>
79+
<version>3.25.1</version>
80+
<scope>compile</scope>
81+
</dependency>
6982

7083
</dependencies>
7184

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/DemoApplication.java

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
package apijson.demo;
1616

17+
import apijson.JSONParser;
18+
import apijson.framework.*;
19+
import com.alibaba.fastjson.JSON;
20+
import com.alibaba.fastjson.JSONArray;
21+
import com.alibaba.fastjson.JSONObject;
22+
import com.alibaba.fastjson.parser.Feature;
1723
import org.springframework.boot.SpringApplication;
18-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
1924
import org.springframework.boot.autoconfigure.SpringBootApplication;
2025
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2126
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
@@ -26,9 +31,8 @@
2631
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2732

2833
import apijson.Log;
29-
import apijson.framework.APIJSONApplication;
30-
import apijson.framework.APIJSONCreator;
31-
import apijson.orm.SQLConfig;
34+
35+
import java.util.List;
3236

3337

3438
/**
@@ -73,12 +77,83 @@ public void addCorsMappings(CorsRegistry registry) {
7377
}
7478

7579
static {
80+
// 使用 fastjson
81+
apijson.JSON.JSON_OBJECT_CLASS = JSONObject.class;
82+
apijson.JSON.JSON_ARRAY_CLASS = JSONArray.class;
83+
84+
final Feature[] DEFAULT_FASTJSON_FEATURES = {Feature.OrderedField, Feature.UseBigDecimal};
85+
apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() {
86+
87+
@Override
88+
public JSONObject createJSONObject() {
89+
return new JSONObject(true);
90+
}
91+
92+
@Override
93+
public JSONArray createJSONArray() {
94+
return new JSONArray();
95+
}
96+
97+
@Override
98+
public String toJSONString(Object obj) {
99+
return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj);
100+
}
101+
102+
@Override
103+
public Object parseJSON(Object json) {
104+
return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
105+
}
106+
107+
@Override
108+
public JSONObject parseObject(Object json) {
109+
return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
110+
}
111+
112+
@Override
113+
public <T> T parseObject(Object json, Class<T> clazz) {
114+
return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES);
115+
}
116+
117+
@Override
118+
public JSONArray parseArray(Object json) {
119+
return JSON.parseArray(toJSONString(json));
120+
}
121+
122+
@Override
123+
public <T> List<T> parseArray(Object json, Class<T> clazz) {
124+
return JSON.parseArray(toJSONString(json), clazz);
125+
}
126+
127+
};
128+
76129
// 使用本项目的自定义处理类
77-
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
130+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long, JSONObject, JSONArray>() {
131+
132+
@Override
133+
public DemoParser createParser() {
134+
return new DemoParser();
135+
}
136+
78137
@Override
79-
public SQLConfig createSQLConfig() {
138+
public DemoFunctionParser createFunctionParser() {
139+
return new DemoFunctionParser();
140+
}
141+
142+
@Override
143+
public DemoVerifier createVerifier() {
144+
return new DemoVerifier();
145+
}
146+
147+
@Override
148+
public DemoSQLConfig createSQLConfig() {
80149
return new DemoSQLConfig();
81150
}
151+
152+
@Override
153+
public DemoSQLExecutor createSQLExecutor() {
154+
return new DemoSQLExecutor();
155+
}
156+
82157
};
83158

84159
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/DemoController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.net.URLDecoder;
1818
import java.util.Map;
1919

20+
import apijson.framework.APIJSONParser;
21+
import com.alibaba.fastjson.JSONArray;
22+
import com.alibaba.fastjson.JSONObject;
2023
import jakarta.servlet.http.HttpSession;
2124

2225
import org.springframework.web.bind.annotation.GetMapping;
@@ -30,7 +33,6 @@
3033
import apijson.RequestMethod;
3134
import apijson.StringUtil;
3235
import apijson.framework.APIJSONController;
33-
import apijson.orm.Parser;
3436

3537

3638
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
@@ -46,10 +48,10 @@
4648
*/
4749
@RestController
4850
@RequestMapping("")
49-
public class DemoController extends APIJSONController<Long> {
51+
public class DemoController extends APIJSONController<Long, JSONObject, JSONArray> {
5052

5153
@Override
52-
public Parser<Long> newParser(HttpSession session, RequestMethod method) {
54+
public APIJSONParser<Long, JSONObject, JSONArray> newParser(HttpSession session, RequestMethod method) {
5355
return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
5456
}
5557

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.demo;
16+
17+
import apijson.RequestMethod;
18+
import apijson.framework.APIJSONFunctionParser;
19+
import apijson.orm.AbstractFunctionParser;
20+
import com.alibaba.fastjson.JSONArray;
21+
import com.alibaba.fastjson.JSONObject;
22+
import jakarta.servlet.http.HttpSession;
23+
24+
25+
/**可远程调用的函数类,用于自定义业务逻辑处理
26+
* 具体见 https://github.com/Tencent/APIJSON/issues/101
27+
* @author Lemon
28+
*/
29+
public class DemoFunctionParser extends APIJSONFunctionParser<Long, JSONObject, JSONArray> {
30+
public static final String TAG = "DemoFunctionParser";
31+
32+
public DemoFunctionParser() {
33+
this(null);
34+
}
35+
public DemoFunctionParser(HttpSession session) {
36+
this(null, null, 0, null, session);
37+
}
38+
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject curObj, HttpSession session) {
39+
super(method, tag, version, curObj, session);
40+
}
41+
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.demo;
16+
17+
import apijson.NotNull;
18+
import apijson.RequestMethod;
19+
import apijson.framework.APIJSONObjectParser;
20+
import apijson.orm.Join;
21+
import apijson.orm.SQLConfig;
22+
import com.alibaba.fastjson.JSONArray;
23+
import com.alibaba.fastjson.JSONObject;
24+
import jakarta.servlet.http.HttpSession;
25+
26+
import java.util.List;
27+
28+
29+
/**对象解析器,用来简化 Parser
30+
* @author Lemon
31+
*/
32+
public class DemoObjectParser extends APIJSONObjectParser<Long, JSONObject, JSONArray> {
33+
34+
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath
35+
, SQLConfig<Long, JSONObject, JSONArray> arrayConfig, boolean isSubquery, boolean isTable
36+
, boolean isArrayMainTable) throws Exception {
37+
super(session, request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable);
38+
}
39+
40+
@Override
41+
public SQLConfig<Long, JSONObject, JSONArray> newSQLConfig(RequestMethod method, String table, String alias
42+
, JSONObject request, List<Join<Long, JSONObject, JSONArray>> joinList, boolean isProcedure) throws Exception {
43+
return DemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
44+
}
45+
46+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.demo;
16+
17+
import apijson.RequestMethod;
18+
import apijson.framework.APIJSONObjectParser;
19+
import apijson.framework.APIJSONParser;
20+
import apijson.orm.*;
21+
import com.alibaba.fastjson.JSONArray;
22+
import com.alibaba.fastjson.JSONObject;
23+
import jakarta.servlet.http.HttpSession;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
29+
/**请求解析器
30+
* 具体见 https://github.com/Tencent/APIJSON/issues/38
31+
* @author Lemon
32+
*/
33+
public class DemoParser extends APIJSONParser<Long, JSONObject, JSONArray> {
34+
35+
public static final Map<String, HttpSession> KEY_MAP;
36+
static {
37+
KEY_MAP = new HashMap<>();
38+
}
39+
40+
public DemoParser() {
41+
super();
42+
}
43+
public DemoParser(RequestMethod method) {
44+
super(method);
45+
}
46+
public DemoParser(RequestMethod method, boolean needVerify) {
47+
super(method, needVerify);
48+
}
49+
50+
// private int maxQueryCount = 200;
51+
// // 可重写来设置最大查询数量
52+
// @Override
53+
// public int getMaxQueryCount() {
54+
// return maxQueryCount;
55+
// }
56+
//
57+
// @Override
58+
// public int getMaxUpdateCount() {
59+
// return 200;
60+
// }
61+
//
62+
// @Override
63+
// public int getMaxObjectCount() {
64+
// return getMaxUpdateCount();
65+
// }
66+
//
67+
// @Override
68+
// public int getMaxSQLCount() {
69+
// return getMaxUpdateCount();
70+
// }
71+
72+
@Override
73+
public APIJSONObjectParser<Long, JSONObject, JSONArray> createObjectParser(JSONObject request, String parentPath
74+
, SQLConfig<Long, JSONObject, JSONArray> arrayConfig
75+
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
76+
return new DemoObjectParser(getSession(), request, parentPath, arrayConfig
77+
, isSubquery, isTable, isArrayMainTable).setMethod(getMethod()).setParser(this);
78+
}
79+
80+
// 实现应用层与数据库共用账号密码,可用于多租户、SQLAuto 等 >>>>>>>>>>>>>>>
81+
82+
}

0 commit comments

Comments
 (0)