Skip to content

Commit ed11005

Browse files
committed
Java:新增 APIJSONDemo-Javax,演示兼容 JDK 1.8 及 SpringBoot 2.x
1 parent 15fa3e3 commit ed11005

File tree

7 files changed

+442
-0
lines changed

7 files changed

+442
-0
lines changed

Diff for: APIJSON-Java-Server/APIJSONDemo-Javax/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

Diff for: APIJSON-Java-Server/APIJSONDemo-Javax/README.md

+7
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

Diff for: APIJSON-Java-Server/APIJSONDemo-Javax/pom.xml

+138
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-javax</artifactId>
8+
<version>7.1.5</version>
9+
10+
<name>APIJSONDemo</name>
11+
<description>Demo project for Testing APIJSON Server based on SpringBoot, compat JDK 1.8~16 and SpringBoot 1.4~2.7</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>1.8</java.version>
17+
</properties>
18+
19+
<dependencies>
20+
<!-- 需要的 APIJSON 相关依赖 -->
21+
<dependency>
22+
<groupId>com.github.Tencent</groupId>
23+
<artifactId>APIJSON</artifactId>
24+
<version>7.1.0</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.github.APIJSON</groupId>
29+
<artifactId>apijson-framework</artifactId>
30+
<version>7.1.5</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.github.APIJSON</groupId>
35+
<artifactId>apijson-router</artifactId>
36+
<version>2.1.5</version>
37+
</dependency>
38+
39+
<!-- JDK 17+, SpringBoot 3.0+ -->
40+
<dependency>
41+
<groupId>jakarta.servlet</groupId>
42+
<artifactId>jakarta.servlet-api</artifactId>
43+
<version>6.0.0</version>
44+
</dependency>
45+
46+
<!-- JDK 1.8~16, SpringBoot 1.4~2.7 -->
47+
<dependency>
48+
<groupId>javax.servlet</groupId>
49+
<artifactId>javax.servlet-api</artifactId>
50+
<version>4.0.1</version>
51+
</dependency>
52+
53+
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一 <<<<<<<<< -->
54+
<dependency>
55+
<groupId>com.github.TommyLemon</groupId>
56+
<artifactId>unitauto-java</artifactId>
57+
<version>3.0.5</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.github.TommyLemon</groupId>
61+
<artifactId>unitauto-jar</artifactId>
62+
<version>3.0.5</version>
63+
</dependency>
64+
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一 >>>>>>>>> -->
65+
66+
67+
<!-- 需要用的数据库 JDBC 驱动 -->
68+
<dependency>
69+
<groupId>mysql</groupId>
70+
<artifactId>mysql-connector-java</artifactId>
71+
<version>8.0.29</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.postgresql</groupId>
75+
<artifactId>postgresql</artifactId>
76+
<version>42.3.8</version>
77+
</dependency>
78+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
79+
80+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-web</artifactId>
84+
<version>2.5.13</version>
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>2.5.13</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>1.8</source>
109+
<target>1.8</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.router.javax.APIJSONRouterApplication;
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
22+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
23+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
27+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
28+
29+
import apijson.Log;
30+
import apijson.framework.javax.APIJSONApplication;
31+
import apijson.framework.javax.APIJSONCreator;
32+
import apijson.orm.SQLConfig;
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+
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
53+
APIJSONRouterApplication.init();
54+
}
55+
56+
// SpringBoot 2.x 自定义端口方式
57+
@Override
58+
public void customize(ConfigurableServletWebServerFactory server) {
59+
server.setPort(8080);
60+
}
61+
62+
// 支持 APIAuto 中 JavaScript 代码跨域请求
63+
@Bean
64+
public WebMvcConfigurer corsConfigurer() {
65+
return new WebMvcConfigurer() {
66+
@Override
67+
public void addCorsMappings(CorsRegistry registry) {
68+
registry.addMapping("/**")
69+
.allowedOriginPatterns("*")
70+
.allowedMethods("*")
71+
.allowCredentials(true)
72+
.maxAge(3600);
73+
}
74+
};
75+
}
76+
77+
static {
78+
// 使用本项目的自定义处理类
79+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
80+
@Override
81+
public SQLConfig createSQLConfig() {
82+
return new DemoSQLConfig();
83+
}
84+
};
85+
86+
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
87+
// try { //加载驱动程序
88+
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
89+
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
90+
// Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
91+
// }
92+
// catch (ClassNotFoundException e) {
93+
// e.printStackTrace();
94+
// Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
95+
// }
96+
//
97+
// try { //加载驱动程序
98+
// Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
99+
// Class.forName("oracle.jdbc.driver.OracleDriver");
100+
// Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
101+
// }
102+
// catch (ClassNotFoundException e) {
103+
// e.printStackTrace();
104+
// Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
105+
// }
106+
107+
}
108+
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 java.net.URLDecoder;
18+
import java.util.Map;
19+
20+
import javax.servlet.http.HttpSession;
21+
22+
import apijson.router.javax.APIJSONRouterController;
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.framework.javax.APIJSONController;
34+
import apijson.orm.Parser;
35+
36+
37+
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
38+
* 具体见 SpringBoot 文档
39+
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
40+
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
41+
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
42+
* <br > 建议全通过HTTP POST来请求:
43+
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
44+
* <br > 2.提高性能 - 无需URL encode和decode
45+
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
46+
* @author Lemon
47+
*/
48+
@RestController
49+
@RequestMapping("")
50+
public class DemoController extends APIJSONRouterController<Long> {
51+
52+
@Override
53+
public Parser<Long> newParser(HttpSession session, RequestMethod method) {
54+
return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
55+
}
56+
57+
@PostMapping("router/{method}/{tag}")
58+
@Override
59+
public String router(@PathVariable("method") String method, @PathVariable("tag") String tag
60+
, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
61+
return super.router(method, tag, params, request, session);
62+
}
63+
64+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
65+
* @param method
66+
* @param request
67+
* @param session
68+
* @return
69+
*/
70+
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
71+
@Override
72+
public String crud(@PathVariable("method") String method, @RequestBody String request, HttpSession session) {
73+
return super.crud(method, request, session);
74+
}
75+
76+
/**全能增删改查接口,可同时进行 增、删、改、查 多种操作,
77+
* 通过 @method: "POST", @gets: { "Privacy":"Privacy-CIRCLE", "User": { "@role":"LOGIN", "tag":"User" } } 等关键词指定
78+
* @param request
79+
* @param session
80+
* @return
81+
*/
82+
@PostMapping(value = "crud") // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突
83+
// @Override
84+
public String crudAll(@RequestBody String request, HttpSession session) {
85+
return newParser(session, RequestMethod.CRUD).parse(request);
86+
}
87+
88+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
89+
* @param method
90+
* @param tag
91+
* @param params
92+
* @param request
93+
* @param session
94+
* @return
95+
*/
96+
@PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
97+
@Override
98+
public String crudByTag(@PathVariable("method") String method, @PathVariable("tag") String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
99+
return super.crudByTag(method, tag, params, request, session);
100+
}
101+
102+
/**获取
103+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
104+
* @param request 只用String,避免encode后未decode
105+
* @param session
106+
* @return
107+
* @see {@link RequestMethod#GET}
108+
*/
109+
@GetMapping("get/{request}")
110+
public String openGet(@PathVariable("request") String request, HttpSession session) {
111+
try {
112+
request = URLDecoder.decode(request, StringUtil.UTF_8);
113+
} catch (Exception e) {
114+
// Parser 会报错
115+
}
116+
return get(request, session);
117+
}
118+
119+
}

0 commit comments

Comments
 (0)