Skip to content

Commit 63b8a8b

Browse files
committed
Java:新增 APIJSONDemo-Elasticsearch
1 parent 87c983f commit 63b8a8b

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ APIJSON-Java-Server/APIJSONDemo-Presto/target
2525
APIJSON-Java-Server/APIJSONBoot-BigData/.idea
2626
APIJSON-Java-Server/APIJSONDemo-ShardingSphere/.idea
2727
APIJSON-Java-Server/APIJSONDemo-ShardingSphere/target
28+
APIJSON-Java-Server/APIJSONDemo-ClickHouse/.idea
29+
APIJSON-Java-Server/APIJSONDemo-Elasticsearch/target
30+
APIJSON-Java-Server/APIJSONDemo-ClickHouse/target
31+
APIJSON-Java-Server/APIJSONDemo-Elasticsearch/.idea
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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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-Elasticsearch</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>org.elasticsearch.plugin</groupId>
48+
<artifactId>x-pack-sql-jdbc</artifactId>
49+
<version>8.4.3</version>
50+
</dependency>
51+
52+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
53+
54+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter-web</artifactId>
58+
<version>2.5.13</version>
59+
</dependency>
60+
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
<configuration>
69+
<fork>true</fork>
70+
<mainClass>apijson.demo.DemoApplication</mainClass>
71+
</configuration>
72+
<executions>
73+
<execution>
74+
<goals>
75+
<goal>repackage</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-compiler-plugin</artifactId>
83+
<configuration>
84+
<source>1.8</source>
85+
<target>1.8</target>
86+
</configuration>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
91+
<repositories>
92+
<!-- APIJSON 必须用到的托管平台 -->
93+
<repository>
94+
<id>jitpack.io</id>
95+
<url>https://jitpack.io</url>
96+
<snapshots>
97+
<enabled>true</enabled>
98+
</snapshots>
99+
</repository>
100+
101+
<repository>
102+
<id>spring-snapshots</id>
103+
<url>https://repo.spring.io/snapshot</url>
104+
<snapshots>
105+
<enabled>true</enabled>
106+
</snapshots>
107+
</repository>
108+
<repository>
109+
<id>spring-milestones</id>
110+
<url>https://repo.spring.io/milestone</url>
111+
</repository>
112+
</repositories>
113+
114+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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, "尝试加载 Elasticsearch 驱动 <<<<<<<<<<<<<<<<<<<<< ");
88+
Class.forName("org.elasticsearch.xpack.sql.jdbc.EsDriver");
89+
Log.d(TAG, "成功加载 Elasticsearch 驱动!>>>>>>>>>>>>>>>>>>>>> ");
90+
}
91+
catch (ClassNotFoundException e) {
92+
e.printStackTrace();
93+
Log.e(TAG, "加载 Presto 驱动失败,请检查 pom.xml 中 EsDriver 版本是否存在以及可用 !!!");
94+
}
95+
96+
}
97+
98+
}
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)