Skip to content

Commit dbe1bc2

Browse files
committed
Java:新增 APIJSONDemo-ClickHouse
1 parent 63b8a8b commit dbe1bc2

File tree

6 files changed

+404
-0
lines changed

6 files changed

+404
-0
lines changed
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: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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>5.4.0</version>
9+
10+
<name>APIJSONDemo-ClickHouse</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>1.8</java.version>
17+
</properties>
18+
19+
<dependencies>
20+
<!-- JDK 11+ 需要,否则启动报错 NoClassDefFoundError: javax/activation/UnsupportedDataTypeException -->
21+
<dependency>
22+
<groupId>javax.activation</groupId>
23+
<artifactId>activation</artifactId>
24+
<version>1.1.1</version>
25+
</dependency>
26+
27+
<!-- 需要的 APIJSON 相关依赖 -->
28+
<dependency>
29+
<groupId>com.github.Tencent</groupId>
30+
<artifactId>APIJSON</artifactId>
31+
<version>5.4.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.github.APIJSON</groupId>
35+
<artifactId>apijson-framework</artifactId>
36+
<version>5.3.0</version>
37+
<exclusions>
38+
<exclusion>
39+
<groupId>com.github.Tecent</groupId>
40+
<artifactId>APIJSON</artifactId>
41+
</exclusion>
42+
</exclusions>
43+
</dependency>
44+
45+
<!-- 需要用的数据库 JDBC 驱动 -->
46+
<dependency>
47+
<groupId>cc.blynk.clickhouse</groupId>
48+
<artifactId>clickhouse4j</artifactId>
49+
<version>1.4.4</version>
50+
</dependency>
51+
<!-- <dependency>-->
52+
<!-- <groupId>ru.yandex.clickhouse</groupId>-->
53+
<!-- <artifactId>clickhouse-jdbc</artifactId>-->
54+
<!-- <version>0.3.1</version>-->
55+
<!-- </dependency>-->
56+
57+
<!-- <dependency>-->
58+
<!-- <groupId>com.github.housepower</groupId>-->
59+
<!-- <artifactId>clickhouse-native-jdbc</artifactId>-->
60+
<!-- <version>1.6-stable</version>-->
61+
<!-- </dependency>-->
62+
<!-- https://mvnrepository.com/artifact/ru.yandex.clickhouse/clickhouse-jdbc -->
63+
<!-- <dependency>-->
64+
<!-- <groupId>ru.yandex.clickhouse</groupId>-->
65+
<!-- <artifactId>clickhouse-jdbc</artifactId>-->
66+
<!-- <version>0.2.4</version>-->
67+
<!-- </dependency>-->
68+
69+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
70+
71+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-web</artifactId>
75+
<version>2.5.13</version>
76+
</dependency>
77+
78+
</dependencies>
79+
80+
<build>
81+
<plugins>
82+
<plugin>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-maven-plugin</artifactId>
85+
<configuration>
86+
<fork>true</fork>
87+
<mainClass>apijson.demo.DemoApplication</mainClass>
88+
</configuration>
89+
<executions>
90+
<execution>
91+
<goals>
92+
<goal>repackage</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-compiler-plugin</artifactId>
100+
<configuration>
101+
<source>1.8</source>
102+
<target>1.8</target>
103+
</configuration>
104+
</plugin>
105+
</plugins>
106+
</build>
107+
108+
<repositories>
109+
<!-- APIJSON 必须用到的托管平台 -->
110+
<repository>
111+
<id>jitpack.io</id>
112+
<url>https://jitpack.io</url>
113+
<snapshots>
114+
<enabled>true</enabled>
115+
</snapshots>
116+
</repository>
117+
118+
<repository>
119+
<id>spring-snapshots</id>
120+
<url>https://repo.spring.io/snapshot</url>
121+
<snapshots>
122+
<enabled>true</enabled>
123+
</snapshots>
124+
</repository>
125+
<repository>
126+
<id>spring-milestones</id>
127+
<url>https://repo.spring.io/milestone</url>
128+
</repository>
129+
</repositories>
130+
131+
</project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 org.springframework.boot.SpringApplication;
18+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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+
import apijson.framework.APIJSONApplication;
30+
import apijson.framework.APIJSONCreator;
31+
import apijson.orm.SQLConfig;
32+
33+
34+
/**
35+
* Demo SpringBoot Application 主应用程序启动类
36+
* 右键这个类 > Run As > Java Application
37+
* 具体见 SpringBoot 文档
38+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
39+
*
40+
* @author Lemon
41+
*/
42+
@Configuration
43+
@SpringBootApplication
44+
@EnableAutoConfiguration
45+
@EnableConfigurationProperties
46+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
47+
public static final String TAG = "DemoApplication";
48+
49+
public static void main(String[] args) throws Exception {
50+
SpringApplication.run(DemoApplication.class, args);
51+
52+
Log.DEBUG = true;
53+
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
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+
try { //加载驱动程序
87+
Log.d(TAG, "尝试加载 clickhouse 驱动 <<<<<<<<<<<<<<<<<<<<< ");
88+
// Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
89+
Class.forName("cc.blynk.clickhouse.ClickHouseDriver");
90+
Log.d(TAG, "成功加载 clickhouse 驱动!>>>>>>>>>>>>>>>>>>>>> ");
91+
}
92+
catch (ClassNotFoundException e) {
93+
e.printStackTrace();
94+
Log.e(TAG, "加载 clickhouse 驱动失败,请检查 pom.xml 中 ru.yandex.clickhouse 版本是否存在以及可用 !!!");
95+
}
96+
97+
}
98+
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 org.springframework.web.bind.annotation.GetMapping;
18+
import org.springframework.web.bind.annotation.PathVariable;
19+
import org.springframework.web.bind.annotation.PostMapping;
20+
import org.springframework.web.bind.annotation.RequestBody;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
import java.net.URLDecoder;
26+
import java.util.Map;
27+
28+
import javax.servlet.http.HttpSession;
29+
30+
import apijson.RequestMethod;
31+
import apijson.StringUtil;
32+
import apijson.framework.APIJSONController;
33+
import apijson.orm.Parser;
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 Parser<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 String method, @RequestBody String request, HttpSession session) {
65+
return super.crud(method, request, session);
66+
}
67+
68+
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
69+
* @param method
70+
* @param tag
71+
* @param params
72+
* @param request
73+
* @param session
74+
* @return
75+
*/
76+
@PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
77+
@Override
78+
public String crudByTag(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
79+
return super.crudByTag(method, tag, params, request, session);
80+
}
81+
82+
/**获取
83+
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
84+
* @param request 只用String,避免encode后未decode
85+
* @param session
86+
* @return
87+
* @see {@link RequestMethod#GET}
88+
*/
89+
@GetMapping("get/{request}")
90+
public String openGet(@PathVariable String request, HttpSession session) {
91+
try {
92+
request = URLDecoder.decode(request, StringUtil.UTF_8);
93+
} catch (Exception e) {
94+
// Parser 会报错
95+
}
96+
return get(request, session);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)