Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/package.json
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/package-lock.json
APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/package-lock.json
/APIJSON-Java-Server/APIJSONDemo-SpringBoot4/.mvn
33 changes: 33 additions & 0 deletions APIJSON-Java-Server/APIJSONDemo-SpringBoot4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
74 changes: 74 additions & 0 deletions APIJSON-Java-Server/APIJSONDemo-SpringBoot4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>apijson.demo</groupId>
<artifactId>apijson-demo</artifactId>
<name>apijson-demo</name>
<description>Demo project for Spring Boot</description>
<version>7.9.0</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.84</version>
</dependency>

<!-- 可使用 libs 目录的 apijson-orm.jar 来替代,两种方式二选一 -->
<dependency>
<groupId>com.github.Tencent</groupId>
<artifactId>APIJSON</artifactId>
<version>7.9.0</version>
</dependency>

<!-- 数据库 JDBC 驱动 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- 数据库 JDBC 驱动 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>4.1.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>4.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>4.1.1</version>
</plugin>
</plugins>
</build>

<repositories>
<!-- APIJSON 必须用到的托管平台 -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package apijson.demo;

import apijson.Log;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Map;


/**
* Demo SpringBoot Application 主应用程序启动类
* 右键这个类 > Run As > Java Application
* 具体见 SpringBoot 文档
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
*
* @author Lemon
*/
@Configuration
@SpringBootApplication
@EnableConfigurationProperties
public class DemoApplication {

public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setDefaultProperties(Map.of("server.port", 8080));
app.run(args);

Log.DEBUG = true; // TODO 上线前改为 false
System.out.println("官方网站: http://apijson.cn");
System.out.println("设计规范: https://github.com/Tencent/APIJSON/blob/master/Document.md#3");
System.out.println("测试链接: http://apijson.cn/api?type=JSON&url=http://localhost:8080/get");
System.out.println("\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 启动完成,试试调用零代码万能通用 API 吧 ^_^ >>>>>>>>>>>>>>>>>>>>>>>>\n");
}


// 支持 APIAuto 中 JavaScript 代码跨域请求
@Bean
public WebMvcConfigurer corsConfig() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowCredentials(true)
.maxAge(3600);
}
};
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package apijson.demo;

import apijson.RequestMethod;
import apijson.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;


/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
* 具体见 SpringBoot 文档
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
* <br > 建议全通过HTTP POST来请求:
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
* <br > 2.提高性能 - 无需URL encode和decode
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
* @author Lemon
*/
@RestController
@RequestMapping("")
public class DemoController { // extends APIJSONController<Long> {

public String parse(RequestMethod method, String request, HttpSession session) {
// TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
return new DemoParser(method).setSession(session).setNeedVerify(false).parse(request);
}


//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

/**获取
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
@PostMapping(value = "get")
public String get(@RequestBody String request, HttpSession session) {
return parse(RequestMethod.GET, request, session);
}


/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param request
* @param session
* @return
*/
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
// @Override
public String crud(@PathVariable String method, @RequestBody String request, HttpSession session) {
return parse(RequestMethod.valueOf(StringUtil.toUpperCase(method)), request, session);
}

/**获取
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
@GetMapping("get/{request}")
public String openGet(@PathVariable String request, HttpSession session) {
try {
request = URLDecoder.decode(request, StringUtil.UTF_8);
} catch (Exception e) {
// Parser 会报错
}
return get(request, session);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package apijson.demo;

import apijson.orm.AbstractFunctionParser;
import com.alibaba.fastjson.JSONObject;

import apijson.RequestMethod;


/**可远程调用的函数类,用于自定义业务逻辑处理
* 具体见 https://github.com/Tencent/APIJSON/issues/101
* @author Lemon
*/
public class DemoFunctionParser extends AbstractFunctionParser<Long> {
public static final String TAG = "DemoFunctionParser";


public DemoFunctionParser() {
this(null, null, 0, null);
}
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request) {
super(method, tag, version, request);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package apijson.demo;

import apijson.orm.AbstractObjectParser;
import com.alibaba.fastjson.JSONObject;

import java.util.List;


import apijson.NotNull;
import apijson.RequestMethod;
import apijson.orm.Join;
import apijson.orm.SQLConfig;
import jakarta.servlet.http.HttpSession;


/**对象解析器,用来简化 Parser
* @author Lemon
*/
public class DemoObjectParser extends AbstractObjectParser<Long> {

public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig arrayConfig
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
super(request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable);
}

@Override
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
return DemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
}


}
Loading