赞
踩
APIJSON是一个关于API和JSON的综合技术或框架,一种专为API设计的JSON网络传输协议,以及基于这套协议实现的ORM库。
{
"user": {
"id": 0, // 通常为自增主键,可省略或设为0
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com"
}
}
GET /User/1
返回结果:
{
"code": 200,
"msg": "success",
"data": {
"User": {
"id": 1,
"name": "John Doe",
"age": 30
}
}
}
{
"user": {
"id": 1,
"email": "newemail@example.com"
}
}
DELETE /User/1
<!-- 需要的 APIJSON 相关依赖 --> <dependency> <groupId>com.github.Tencent</groupId> <artifactId>APIJSON</artifactId> <version>6.3.0</version> </dependency> <dependency> <groupId>com.github.APIJSON</groupId> <artifactId>apijson-framework</artifactId> <version>6.3.0</version> </dependency> <!-- 需要用的数据库 JDBC 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <!-- 需要用的 SpringBoot 框架,1.4.0 以上 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.43</version> </dependency>
/*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 org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; 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 apijson.Log; import apijson.framework.APIJSONApplication; import apijson.framework.APIJSONCreator; import apijson.orm.SQLConfig; /** * Demo SpringBoot Application 主应用程序启动类 */ @Configuration @SpringBootApplication @EnableConfigurationProperties public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { public static void main(String[] args) throws Exception { SpringApplication.run(DemoApplication.class, args); Log.DEBUG = true; APIJSONApplication.init(false); } // SpringBoot 2.x 自定义端口方式 @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(8080); } // 支持 APIAuto 中 JavaScript 代码跨域请求 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedMethods("*") .allowCredentials(true) .maxAge(3600); } }; } static { // 使用本项目的自定义处理类 APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() { @Override public SQLConfig createSQLConfig() { return new DemoSQLConfig(); } }; } }
/*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 com.alibaba.fastjson.annotation.JSONField; import apijson.framework.APIJSONSQLConfig; /**SQL 配置 */ public class DemoSQLConfig extends APIJSONSQLConfig { static { DEFAULT_DATABASE = DATABASE_MYSQL; // TODO 默认数据库类型,改成你自己的 DEFAULT_SCHEMA = "apijson"; // TODO 默认数据库名/模式,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: public, SQL Server: dbo, Oracle: //表名映射,隐藏真实表名,对安全要求很高的表可以这么做 TABLE_KEY_MAP.put("User", "apijson_user"); TABLE_KEY_MAP.put("Privacy", "apijson_privacy"); } @Override public String getDBVersion() { return "5.7.22"; // "8.0.11"; // TODO 改成你自己的 MySQL 或 PostgreSQL 数据库版本号 // MYSQL 8 和 7 使用的 JDBC 配置不一样 } @JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息 @Override public String getDBUri() { return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000 } @JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息 @Override public String getDBAccount() { return "root"; // TODO 改成你自己的 } @JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息 @Override public String getDBPassword() { return "123456"; // TODO 改成你自己的,TiDB 可以当成 MySQL 使用, 默认密码为空字符串 "" } }
/*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 java.net.URLDecoder; import java.util.Map; import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import apijson.RequestMethod; import apijson.StringUtil; import apijson.framework.APIJSONController; import apijson.orm.Parser; /**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理 */ @RestController @RequestMapping("") public class DemoController extends APIJSONController<Long> { @Override public Parser<Long> newParser(HttpSession session, RequestMethod method) { return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启 } /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率 */ @PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud") @Override public String crud(@PathVariable String method, @RequestBody String request, HttpSession session) { return super.crud(method, request, session); } /**全能增删改查接口,可同时进行 增、删、改、查 多种操作, * 通过 @method: "POST", @gets: { "Privacy":"Privacy-CIRCLE", "User": { "@role":"LOGIN", "tag":"User" } } 等关键词指定 */ @PostMapping(value = "crud") // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突 // @Override public String crudAll(@RequestBody String request, HttpSession session) { return newParser(session, RequestMethod.CRUD).parse(request); } /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率 */ @PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud") @Override public String crudByTag(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) { return super.crudByTag(method, tag, params, request, session); } }
[初始化脚本](https://gitee.com/APIJSON/APIJSON-Demo/blob/master/MySQL/sys.sql)
curl --location --request POST 'http://localhost:8080/get' \
--header 'Content-Type: application/json' \
--data-raw '{
"User[]":{
"count":3,
"User":[]
}
}'
{ "User[]": [ [], { "$ref": "$.User\\[\\][0]" }, { "$ref": "$.User\\[\\][0]" } ], "ok": true, "code": 200, "msg": "success", "debug:info|help": " \n提 bug 请发请求和响应的【完整截屏】,没图的自行解决! \n开发者有限的时间和精力主要放在【维护项目源码和文档】上! \n【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!! \n【态度 不文明/不友善】的可能会被踢出群,问题也可能不予解答!!! \n\n **环境信息** \n系统: Windows 10 10.0 \n数据库: DEFAULT_DATABASE = MYSQL \nJDK: 1.8.0_101 amd64 \nAPIJSON: 6.3.0 \n \n【常见问题】:https://github.com/Tencent/APIJSON/issues/36 \n【通用文档】:https://github.com/Tencent/APIJSON/blob/master/Document.md \n【视频教程】:https://search.bilibili.com/all?keyword=APIJSON", "time": 1717495293436, "sql:generate|cache|execute|maxExecute": "0|0|0|200", "depth:count|max": "2|5", "time:start|duration|end|parse|sql": "1717495293434|2|1717495293436|2|0" }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。