Visitar URL original
Merge pull request #39 from APIJSON/master · APIJSON/APIJSON-Demo@a0b34cb · GitHub
Skip to content

Commit a0b34cb

Browse files
authored
Merge pull request #39 from APIJSON/master
Java:新增 SpringBoot 4 的 Demo:APIJSONDemo-SpringBoot4
2 parents b2f58fc + 3e0c92b commit a0b34cb

12 files changed

Lines changed: 695 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/
1313
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/package.json
1414
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/package-lock.json
1515
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/package-lock.json
16+
/APIJSON-Java-Server/APIJSONDemo-SpringBoot4/.mvn
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>4.1.1</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>apijson.demo</groupId>
12+
<artifactId>apijson-demo</artifactId>
13+
<name>apijson-demo</name>
14+
<description>Demo project for Spring Boot</description>
15+
<version>7.9.0</version>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>com.alibaba</groupId>
23+
<artifactId>fastjson</artifactId>
24+
<version>1.2.84</version>
25+
</dependency>
26+
27+
<!-- 可使用 libs 目录的 apijson-orm.jar 来替代,两种方式二选一 -->
28+
<dependency>
29+
<groupId>com.github.Tencent</groupId>
30+
<artifactId>APIJSON</artifactId>
31+
<version>7.9.0</version>
32+
</dependency>
33+
34+
<!-- 数据库 JDBC 驱动 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
35+
<dependency>
36+
<groupId>mysql</groupId>
37+
<artifactId>mysql-connector-java</artifactId>
38+
<version>8.0.29</version>
39+
</dependency>
40+
<!-- 数据库 JDBC 驱动 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
<version>4.1.1</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<version>4.1.1</version>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
<version>4.1.1</version>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
<repositories>
67+
<!-- APIJSON 必须用到的托管平台 -->
68+
<repository>
69+
<id>jitpack.io</id>
70+
<url>https://jitpack.io</url>
71+
</repository>
72+
</repositories>
73+
74+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.Log;
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.context.ConfigurableApplicationContext;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
25+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
26+
27+
import java.util.Map;
28+
29+
30+
/**
31+
* Demo SpringBoot Application 主应用程序启动类
32+
* 右键这个类 > Run As > Java Application
33+
* 具体见 SpringBoot 文档
34+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
35+
*
36+
* @author Lemon
37+
*/
38+
@Configuration
39+
@SpringBootApplication
40+
@EnableConfigurationProperties
41+
public class DemoApplication {
42+
43+
public static void main(String[] args) throws Exception {
44+
SpringApplication app = new SpringApplication(DemoApplication.class);
45+
app.setDefaultProperties(Map.of("server.port", 8080));
46+
app.run(args);
47+
48+
Log.DEBUG = true; // TODO 上线前改为 false
49+
System.out.println("官方网站: http://apijson.cn");
50+
System.out.println("设计规范: https://github.com/Tencent/APIJSON/blob/master/Document.md#3");
51+
System.out.println("测试链接: http://apijson.cn/api?type=JSON&url=http://localhost:8080/get");
52+
System.out.println("\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 启动完成,试试调用零代码万能通用 API 吧 ^_^ >>>>>>>>>>>>>>>>>>>>>>>>\n");
53+
}
54+
55+
56+
// 支持 APIAuto 中 JavaScript 代码跨域请求
57+
@Bean
58+
public WebMvcConfigurer corsConfig() {
59+
return new WebMvcConfigurer() {
60+
@Override
61+
public void addCorsMappings(CorsRegistry registry) {
62+
registry.addMapping("/**")
63+
.allowedOriginPatterns("*")
64+
.allowedMethods("*")
65+
.allowCredentials(true)
66+
.maxAge(3600);
67+
}
68+
};
69+
}
70+
71+
72+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.StringUtil;
19+
import jakarta.servlet.http.HttpSession;
20+
import org.springframework.web.bind.annotation.*;
21+
22+
import java.net.URLDecoder;
23+
24+
25+
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
26+
* 具体见 SpringBoot 文档
27+
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
28+
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
29+
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
30+
* <br > 建议全通过HTTP POST来请求:
31+
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
32+
* <br > 2.提高性能 - 无需URL encode和decode
33+
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
34+
* @author Lemon
35+
*/
36+
@RestController
37+
@RequestMapping("")
38+
public class DemoController { // extends APIJSONController<Long> {
39+
40+
public String parse(RequestMethod method, String request, HttpSession session) {
41+
// TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
42+
return new DemoParser(method).setSession(session).setNeedVerify(false).parse(request);
43+
}
44+
45+
46+
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
47+
48+
/**获取
49+
* @param request 只用String,避免encode后未decode
50+
* @param session
51+
* @return
52+
* @see {@link RequestMethod#GET}
53+
*/
54+
@PostMapping(value = "get")
55+
public String get(@RequestBody String request, HttpSession session) {
56+
return parse(RequestMethod.GET, request, session);
57+
}
58+
59+
60+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
61+
* @param method
62+
* @param request
63+
* @param session
64+
* @return
65+
*/
66+
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
67+
// @Override
68+
public String crud(@PathVariable String method, @RequestBody String request, HttpSession session) {
69+
return parse(RequestMethod.valueOf(StringUtil.toUpperCase(method)), request, session);
70+
}
71+
72+
/**获取
73+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
74+
* @param request 只用String,避免encode后未decode
75+
* @param session
76+
* @return
77+
* @see {@link RequestMethod#GET}
78+
*/
79+
@GetMapping("get/{request}")
80+
public String openGet(@PathVariable String request, HttpSession session) {
81+
try {
82+
request = URLDecoder.decode(request, StringUtil.UTF_8);
83+
} catch (Exception e) {
84+
// Parser 会报错
85+
}
86+
return get(request, session);
87+
}
88+
89+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.orm.AbstractFunctionParser;
18+
import com.alibaba.fastjson.JSONObject;
19+
20+
import apijson.RequestMethod;
21+
22+
23+
/**可远程调用的函数类,用于自定义业务逻辑处理
24+
* 具体见 https://github.com/Tencent/APIJSON/issues/101
25+
* @author Lemon
26+
*/
27+
public class DemoFunctionParser extends AbstractFunctionParser<Long> {
28+
public static final String TAG = "DemoFunctionParser";
29+
30+
31+
public DemoFunctionParser() {
32+
this(null, null, 0, null);
33+
}
34+
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request) {
35+
super(method, tag, version, request);
36+
}
37+
38+
39+
}
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.orm.AbstractObjectParser;
18+
import com.alibaba.fastjson.JSONObject;
19+
20+
import java.util.List;
21+
22+
23+
import apijson.NotNull;
24+
import apijson.RequestMethod;
25+
import apijson.orm.Join;
26+
import apijson.orm.SQLConfig;
27+
import jakarta.servlet.http.HttpSession;
28+
29+
30+
/**对象解析器,用来简化 Parser
31+
* @author Lemon
32+
*/
33+
public class DemoObjectParser extends AbstractObjectParser<Long> {
34+
35+
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig arrayConfig
36+
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
37+
super(request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable);
38+
}
39+
40+
@Override
41+
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
42+
return DemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
43+
}
44+
45+
46+
}

0 commit comments

Comments
 (0)