Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 750ba8e

Browse files
Java:新增 fastjson2 的 Demo APIJSONDemo-fastjson2
1 parent 08e42ac commit 750ba8e

File tree

12 files changed

+679
-0
lines changed

12 files changed

+679
-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: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.5</version>
9+
10+
<name>APIJSONDemo</name>
11+
<description>Demo project for Testing APIJSON Server based on 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+
<dependency>
24+
<groupId>com.alibaba.fastjson2</groupId>
25+
<artifactId>fastjson2</artifactId>
26+
<version>2.0.57</version>
27+
</dependency>
28+
29+
<!-- 需要的 APIJSON 相关依赖 -->
30+
<dependency>
31+
<groupId>com.github.Tencent</groupId>
32+
<artifactId>APIJSON</artifactId>
33+
<version>8.0.2</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.github.APIJSON</groupId>
37+
<artifactId>apijson-framework</artifactId>
38+
<version>7.2.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.github.APIJSON</groupId>
42+
<artifactId>apijson-fastjson2</artifactId>
43+
<version>1.0.2</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>jakarta.servlet</groupId>
47+
<artifactId>jakarta.servlet-api</artifactId>
48+
<version>6.0.0</version>
49+
</dependency>
50+
51+
<!-- 需要用的数据库 JDBC 驱动 -->
52+
<dependency>
53+
<groupId>com.mysql</groupId>
54+
<artifactId>mysql-connector-j</artifactId>
55+
<version>8.4.0</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.postgresql</groupId>
59+
<artifactId>postgresql</artifactId>
60+
<version>42.7.2</version>
61+
</dependency>
62+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
63+
64+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
<version>3.2.5</version>
69+
<exclusions>
70+
<exclusion>
71+
<groupId>ch.qos.logback</groupId>
72+
<artifactId>logback-classic</artifactId>
73+
</exclusion>
74+
<!-- <exclusion>-->
75+
<!-- <groupId>org.slf4j</groupId>-->
76+
<!-- <artifactId>slf4j-simple</artifactId>-->
77+
<!-- </exclusion>-->
78+
</exclusions>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.google.protobuf</groupId>
82+
<artifactId>protobuf-java</artifactId>
83+
<version>3.25.1</version>
84+
<scope>compile</scope>
85+
</dependency>
86+
87+
</dependencies>
88+
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-maven-plugin</artifactId>
94+
<version>3.2.5</version>
95+
<executions>
96+
<execution>
97+
<goals>
98+
<goal>repackage</goal>
99+
</goals>
100+
</execution>
101+
</executions>
102+
</plugin>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-compiler-plugin</artifactId>
106+
<version>3.8.1</version>
107+
<configuration>
108+
<source>17</source>
109+
<target>17</target>
110+
</configuration>
111+
</plugin>
112+
</plugins>
113+
</build>
114+
115+
<repositories>
116+
<!-- APIJSON 必须用到的托管平台 -->
117+
<repository>
118+
<id>jitpack.io</id>
119+
<url>https://jitpack.io</url>
120+
<snapshots>
121+
<enabled>true</enabled>
122+
</snapshots>
123+
</repository>
124+
125+
<repository>
126+
<id>spring-snapshots</id>
127+
<url>https://repo.spring.io/snapshot</url>
128+
<snapshots>
129+
<enabled>true</enabled>
130+
</snapshots>
131+
</repository>
132+
<repository>
133+
<id>spring-milestones</id>
134+
<url>https://repo.spring.io/milestone</url>
135+
</repository>
136+
</repositories>
137+
138+
</project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*Copyright ©2025 APIJSON(https://github.com/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.fastjson2.*;
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+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
109+
// try { //加载驱动程序
110+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
111+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
112+
// Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
113+
// }
114+
// catch (ClassNotFoundException e) {
115+
// e.printStackTrace();
116+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
117+
// }
118+
//
119+
// try { //加载驱动程序
120+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
121+
// Class.forName("oracle.jdbc.driver.OracleDriver");
122+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
123+
// }
124+
// catch (ClassNotFoundException e) {
125+
// e.printStackTrace();
126+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
127+
// }
128+
129+
}
130+
131+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*Copyright ©2025 APIJSON(https://github.com/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 java.net.URLDecoder;
18+
import java.util.Map;
19+
20+
import apijson.fastjson2.APIJSONParser;
21+
import jakarta.servlet.http.HttpSession;
22+
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.PathVariable;
25+
import org.springframework.web.bind.annotation.PostMapping;
26+
import org.springframework.web.bind.annotation.RequestBody;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
import org.springframework.web.bind.annotation.RequestParam;
29+
import org.springframework.web.bind.annotation.RestController;
30+
31+
import apijson.RequestMethod;
32+
import apijson.StringUtil;
33+
import apijson.fastjson2.APIJSONController;
34+
35+
36+
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
37+
* 具体见 SpringBoot 文档
38+
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
39+
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
40+
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
41+
* <br > 建议全通过HTTP POST来请求:
42+
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
43+
* <br > 2.提高性能 - 无需URL encode和decode
44+
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
45+
* @author Lemon
46+
*/
47+
@RestController
48+
@RequestMapping("")
49+
public class DemoController extends APIJSONController<Long> {
50+
51+
@Override
52+
public APIJSONParser<Long> newParser(HttpSession session, RequestMethod method) {
53+
return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
54+
}
55+
56+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
57+
* @param method
58+
* @param request
59+
* @param session
60+
* @return
61+
*/
62+
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
63+
@Override
64+
public String crud(@PathVariable("method") String method, @RequestBody String request, HttpSession session) {
65+
return super.crud(method, request, session);
66+
}
67+
68+
/**全能增删改查接口,可同时进行 增、删、改、查 多种操作,
69+
* 通过 @method: "POST", @gets: { "Privacy":"Privacy-CIRCLE", "User": { "@role":"LOGIN", "tag":"User" } } 等关键词指定
70+
* @param request
71+
* @param session
72+
* @return
73+
*/
74+
@PostMapping(value = "crud") // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突
75+
// @Override
76+
public String crudAll(@RequestBody String request, HttpSession session) {
77+
return newParser(session, RequestMethod.CRUD).parse(request);
78+
}
79+
80+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
81+
* @param method
82+
* @param tag
83+
* @param params
84+
* @param request
85+
* @param session
86+
* @return
87+
*/
88+
@PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
89+
@Override
90+
public String crudByTag(@PathVariable("method") String method, @PathVariable("tag") String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
91+
return super.crudByTag(method, tag, params, request, session);
92+
}
93+
94+
/**获取
95+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
96+
* @param request 只用String,避免encode后未decode
97+
* @param session
98+
* @return
99+
* @see {@link RequestMethod#GET}
100+
*/
101+
@GetMapping("get/{request}")
102+
public String openGet(@PathVariable("request") String request, HttpSession session) {
103+
try {
104+
request = URLDecoder.decode(request, StringUtil.UTF_8);
105+
} catch (Exception e) {
106+
// Parser 会报错
107+
}
108+
return get(request, session);
109+
}
110+
111+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /