Skip to content

Commit f1d0c69

Browse files
committed
Java:新增 Jackson Demo APIJSONDemo-Jackson
1 parent b9f08cb commit f1d0c69

File tree

13 files changed

+723
-1
lines changed

13 files changed

+723
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void addCorsMappings(CorsRegistry registry) {
105105
}
106106

107107
static {
108-
//// 使用 gjson
108+
//// 使用 gson
109109
//apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() {
110110
//
111111
// @Override
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 Jackson 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-jackson</artifactId>
42+
<version>1.0.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.fasterxml.jackson.core</groupId>
46+
<artifactId>jackson-databind</artifactId>
47+
<version>2.20.0</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: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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.jackson.*;
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+
31+
/**
32+
* Demo SpringBoot Application 主应用程序启动类
33+
* 右键这个类 > Run As > Java Application
34+
* 具体见 SpringBoot 文档
35+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
36+
*
37+
* @author Lemon
38+
*/
39+
@Configuration
40+
@SpringBootApplication
41+
@EnableConfigurationProperties
42+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
43+
44+
public static void main(String[] args) throws Exception {
45+
SpringApplication.run(DemoApplication.class, args);
46+
47+
Log.DEBUG = true;
48+
49+
// 使用本项目的自定义处理类
50+
APIJSONCreator<Long> creator = new APIJSONCreator<Long>() {
51+
52+
@Override
53+
public DemoParser createParser() {
54+
return new DemoParser();
55+
}
56+
57+
@Override
58+
public DemoFunctionParser createFunctionParser() {
59+
return new DemoFunctionParser();
60+
}
61+
62+
@Override
63+
public DemoVerifier createVerifier() {
64+
return new DemoVerifier();
65+
}
66+
67+
@Override
68+
public DemoSQLConfig createSQLConfig() {
69+
return new DemoSQLConfig();
70+
}
71+
72+
@Override
73+
public DemoSQLExecutor createSQLExecutor() {
74+
return new DemoSQLExecutor();
75+
}
76+
77+
};
78+
79+
APIJSONApplication.init(false, creator); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
80+
}
81+
82+
// SpringBoot 2.x 自定义端口方式
83+
@Override
84+
public void customize(ConfigurableServletWebServerFactory server) {
85+
server.setPort(8080);
86+
}
87+
88+
// 支持 APIAuto 中 JavaScript 代码跨域请求
89+
@Bean
90+
public WebMvcConfigurer corsConfigurer() {
91+
return new WebMvcConfigurer() {
92+
@Override
93+
public void addCorsMappings(CorsRegistry registry) {
94+
registry.addMapping("/**")
95+
.allowedOriginPatterns("*")
96+
.allowedMethods("*")
97+
.allowCredentials(true)
98+
.maxAge(3600);
99+
}
100+
};
101+
}
102+
103+
static {
104+
//// 使用 jackson
105+
//apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() {
106+
//
107+
// @Override
108+
// public JSONObject createJSONObject() {
109+
// return new JSONObject(true);
110+
// }
111+
//
112+
// @Override
113+
// public JSONArray createJSONArray() {
114+
// return new JSONArray();
115+
// }
116+
//
117+
// @Override
118+
// public String toJSONString(Object obj, boolean format) {
119+
// return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj);
120+
// }
121+
//
122+
// @Override
123+
// public Object parseJSON(Object json) {
124+
// return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
125+
// }
126+
//
127+
// @Override
128+
// public JSONObject parseObject(Object json) {
129+
// return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
130+
// }
131+
//
132+
// @Override
133+
// public <T> T parseObject(Object json, Class<T> clazz) {
134+
// return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES);
135+
// }
136+
//
137+
// @Override
138+
// public JSONArray parseArray(Object json) {
139+
// return JSON.parseArray(toJSONString(json));
140+
// }
141+
//
142+
// @Override
143+
// public <T> List<T> parseArray(Object json, Class<T> clazz) {
144+
// return JSON.parseArray(toJSONString(json), clazz);
145+
// }
146+
//
147+
//};
148+
149+
150+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
151+
// try { //加载驱动程序
152+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
153+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
154+
// Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
155+
// }
156+
// catch (ClassNotFoundException e) {
157+
// e.printStackTrace();
158+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
159+
// }
160+
//
161+
// try { //加载驱动程序
162+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
163+
// Class.forName("oracle.jdbc.driver.OracleDriver");
164+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
165+
// }
166+
// catch (ClassNotFoundException e) {
167+
// e.printStackTrace();
168+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
169+
// }
170+
171+
}
172+
173+
}

0 commit comments

Comments
 (0)