Skip to content

Commit 0a10bc0

Browse files
committed
新增 Gson 版 Demo APIJSONDemo-Gson
1 parent c0288fe commit 0a10bc0

File tree

12 files changed

+726
-0
lines changed

12 files changed

+726
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# APIJSONDemo
2+
3+
APIJSON + SpringBoot 初级使用的最简单 Demo
4+
5+
### 运行
6+
7+
右键 DemoApplication > Run As > Java Application
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apijson.demo</groupId>
7+
<artifactId>apijson-demo</artifactId>
8+
<version>7.1.8</version>
9+
10+
<name>APIJSONDemo</name>
11+
<description>Demo project for Testing APIJSON Server based on Gson and SpringBoot</description>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<java.version>17</java.version>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.target>17</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<!-- 需要的 APIJSON 相关依赖 -->
24+
<dependency>
25+
<groupId>com.github.Tencent</groupId>
26+
<artifactId>APIJSON</artifactId>
27+
<version>8.0.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.github.APIJSON</groupId>
31+
<artifactId>apijson-framework</artifactId>
32+
<version>7.2.2</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>jakarta.servlet</groupId>
36+
<artifactId>jakarta.servlet-api</artifactId>
37+
<version>6.0.0</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.github.APIJSON</groupId>
41+
<artifactId>apijson-gson</artifactId>
42+
<version>1.0.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.google.code.gson</groupId>
46+
<artifactId>gson</artifactId>
47+
<version>2.13.1</version>
48+
</dependency>
49+
50+
<!-- 需要用的数据库 JDBC 驱动 -->
51+
<dependency>
52+
<groupId>com.mysql</groupId>
53+
<artifactId>mysql-connector-j</artifactId>
54+
<version>9.2.0</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.postgresql</groupId>
58+
<artifactId>postgresql</artifactId>
59+
<version>42.7.2</version>
60+
</dependency>
61+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
62+
63+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-web</artifactId>
67+
<version>3.2.5</version>
68+
<exclusions>
69+
<exclusion>
70+
<groupId>ch.qos.logback</groupId>
71+
<artifactId>logback-classic</artifactId>
72+
</exclusion>
73+
<!-- <exclusion>-->
74+
<!-- <groupId>org.slf4j</groupId>-->
75+
<!-- <artifactId>slf4j-simple</artifactId>-->
76+
<!-- </exclusion>-->
77+
</exclusions>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.google.protobuf</groupId>
81+
<artifactId>protobuf-java</artifactId>
82+
<version>3.25.1</version>
83+
<scope>compile</scope>
84+
</dependency>
85+
86+
</dependencies>
87+
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.springframework.boot</groupId>
92+
<artifactId>spring-boot-maven-plugin</artifactId>
93+
<version>3.2.5</version>
94+
<executions>
95+
<execution>
96+
<goals>
97+
<goal>repackage</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-compiler-plugin</artifactId>
105+
<version>3.8.1</version>
106+
<configuration>
107+
<source>17</source>
108+
<target>17</target>
109+
</configuration>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
114+
<repositories>
115+
<!-- APIJSON 必须用到的托管平台 -->
116+
<repository>
117+
<id>jitpack.io</id>
118+
<url>https://jitpack.io</url>
119+
<snapshots>
120+
<enabled>true</enabled>
121+
</snapshots>
122+
</repository>
123+
124+
<repository>
125+
<id>spring-snapshots</id>
126+
<url>https://repo.spring.io/snapshot</url>
127+
<snapshots>
128+
<enabled>true</enabled>
129+
</snapshots>
130+
</repository>
131+
<repository>
132+
<id>spring-milestones</id>
133+
<url>https://repo.spring.io/milestone</url>
134+
</repository>
135+
</repositories>
136+
137+
</project>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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.gson.*;
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
22+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
26+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
27+
28+
import apijson.Log;
29+
30+
import java.util.ArrayList;
31+
import java.util.LinkedHashMap;
32+
import java.util.List;
33+
34+
35+
/**
36+
* Demo SpringBoot Application 主应用程序启动类
37+
* 右键这个类 > Run As > Java Application
38+
* 具体见 SpringBoot 文档
39+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
40+
*
41+
* @author Lemon
42+
*/
43+
@Configuration
44+
@SpringBootApplication
45+
@EnableConfigurationProperties
46+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
47+
48+
public static void main(String[] args) throws Exception {
49+
SpringApplication.run(DemoApplication.class, args);
50+
51+
Log.DEBUG = true;
52+
53+
// 使用本项目的自定义处理类
54+
APIJSONCreator<Long> creator = new APIJSONCreator<Long>() {
55+
56+
@Override
57+
public DemoParser createParser() {
58+
return new DemoParser();
59+
}
60+
61+
@Override
62+
public DemoFunctionParser createFunctionParser() {
63+
return new DemoFunctionParser();
64+
}
65+
66+
@Override
67+
public DemoVerifier createVerifier() {
68+
return new DemoVerifier();
69+
}
70+
71+
@Override
72+
public DemoSQLConfig createSQLConfig() {
73+
return new DemoSQLConfig();
74+
}
75+
76+
@Override
77+
public DemoSQLExecutor createSQLExecutor() {
78+
return new DemoSQLExecutor();
79+
}
80+
81+
};
82+
83+
APIJSONApplication.init(false, creator); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
84+
}
85+
86+
// SpringBoot 2.x 自定义端口方式
87+
@Override
88+
public void customize(ConfigurableServletWebServerFactory server) {
89+
server.setPort(8080);
90+
}
91+
92+
// 支持 APIAuto 中 JavaScript 代码跨域请求
93+
@Bean
94+
public WebMvcConfigurer corsConfigurer() {
95+
return new WebMvcConfigurer() {
96+
@Override
97+
public void addCorsMappings(CorsRegistry registry) {
98+
registry.addMapping("/**")
99+
.allowedOriginPatterns("*")
100+
.allowedMethods("*")
101+
.allowCredentials(true)
102+
.maxAge(3600);
103+
}
104+
};
105+
}
106+
107+
static {
108+
//// 使用 gjson
109+
//apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() {
110+
//
111+
// @Override
112+
// public JSONObject createJSONObject() {
113+
// return new JSONObject(true);
114+
// }
115+
//
116+
// @Override
117+
// public JSONArray createJSONArray() {
118+
// return new JSONArray();
119+
// }
120+
//
121+
// @Override
122+
// public String toJSONString(Object obj, boolean format) {
123+
// return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj);
124+
// }
125+
//
126+
// @Override
127+
// public Object parseJSON(Object json) {
128+
// return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
129+
// }
130+
//
131+
// @Override
132+
// public JSONObject parseObject(Object json) {
133+
// return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
134+
// }
135+
//
136+
// @Override
137+
// public <T> T parseObject(Object json, Class<T> clazz) {
138+
// return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES);
139+
// }
140+
//
141+
// @Override
142+
// public JSONArray parseArray(Object json) {
143+
// return JSON.parseArray(toJSONString(json));
144+
// }
145+
//
146+
// @Override
147+
// public <T> List<T> parseArray(Object json, Class<T> clazz) {
148+
// return JSON.parseArray(toJSONString(json), clazz);
149+
// }
150+
//
151+
//};
152+
153+
154+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
155+
// try { //加载驱动程序
156+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
157+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
158+
// Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
159+
// }
160+
// catch (ClassNotFoundException e) {
161+
// e.printStackTrace();
162+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
163+
// }
164+
//
165+
// try { //加载驱动程序
166+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
167+
// Class.forName("oracle.jdbc.driver.OracleDriver");
168+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
169+
// }
170+
// catch (ClassNotFoundException e) {
171+
// e.printStackTrace();
172+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
173+
// }
174+
175+
}
176+
177+
}

0 commit comments

Comments
 (0)