All Projects → fengwenyi → APIExample

fengwenyi / APIExample

Licence: MIT license
以API接口开发为例,探索开发的最佳实践

Programming Languages

java
68154 projects - #9 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to APIExample

Perfect Ssm
🍇更完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis)
Stars: ✭ 606 (+893.44%)
Mutual labels:  restful-api, springmvc
Yfax Parent
SprintBoot开发的Rest API接口项目实战,集成了拦截器,日志处理,mysql,mybatis, oauth2.0, spring secutity等,已投入生产线上使用。应用了https://github.com/hemin1003/spring-boot-study 所含功能。配套的后台管理系统实战见:https://github.com/hemin1003/aylson-parent
Stars: ✭ 271 (+344.26%)
Mutual labels:  restful-api, springmvc
react-nodejs-mongodb-crud
👨‍💻 Fullstack web app built with MongoDB, NodeJs, React and Redux. Features: Protected routes client/server side, MaterialUI layout
Stars: ✭ 91 (+49.18%)
Mutual labels:  restful-api
dragonshard
Java server framework based on springboot2
Stars: ✭ 15 (-75.41%)
Mutual labels:  springmvc
XCloud
Official Code for Paper <XCloud: Design and Implementation of AI Cloud Platform with RESTful API Service> (arXiv1912.10344)
Stars: ✭ 58 (-4.92%)
Mutual labels:  restful-api
seal
django-base-templates 主要为 django 开发DEMO, 支持 非前后端分离 和 前后端分离模式 。
Stars: ✭ 118 (+93.44%)
Mutual labels:  restful-api
MusicDatabase-API
This project is able to manage your songs, artists, albums and more by RESTful API. Developed with ASP.NET Core 2.0 & EF Core and used PostgreSQL Database Provider. Implemented Swagger to project.
Stars: ✭ 18 (-70.49%)
Mutual labels:  restful-api
Liquid-Application-Framework
Liquid Application Framework documentation, useful links and sample project
Stars: ✭ 467 (+665.57%)
Mutual labels:  restful-api
Java-CS-Record
记录准备春招实习过程中,学习与复习的知识(模块化整理,非面试题速成)。注:暂停更新,后续请移步博客
Stars: ✭ 73 (+19.67%)
Mutual labels:  springmvc
easy-gin
一套基于 Gin 框架的 MVC 脚手架,使用 govendor 包管理
Stars: ✭ 74 (+21.31%)
Mutual labels:  restful-api
nest-admin
NestJs CRUD for RESTful API使用 nestjs + mysql + typeorm + redis + jwt + swagger 企业中后台管理系统项目RBAC权限管理(细粒度到按钮)、实现单点登录等。
Stars: ✭ 165 (+170.49%)
Mutual labels:  restful-api
server
AuthzForce Server (Community Edition)
Stars: ✭ 48 (-21.31%)
Mutual labels:  restful-api
today-web
😐 A Java library for building web applications
Stars: ✭ 33 (-45.9%)
Mutual labels:  springmvc
docker-node-express-boilerplate
Boilerplate for quickly bootstrapping production-ready RESTful APIs / microservices
Stars: ✭ 113 (+85.25%)
Mutual labels:  restful-api
buzzyblog
React + WordPress REST API, a new endeavor to provide a better experience to content creators, web masters and digital marketers, etc
Stars: ✭ 50 (-18.03%)
Mutual labels:  restful-api
rest node api
用node实现restful风格的api
Stars: ✭ 55 (-9.84%)
Mutual labels:  restful-api
nestjs-rest-sample
NestJS RESTful APIs Sample
Stars: ✭ 204 (+234.43%)
Mutual labels:  restful-api
http4s-good-practices
Collection of what I consider good practices in Http4s (WIP)
Stars: ✭ 74 (+21.31%)
Mutual labels:  restful-api
SSM Project
这个一个SSM+shiro+maven+bootstrap的项目实战,经典的图书管理系统。
Stars: ✭ 30 (-50.82%)
Mutual labels:  springmvc
nocodb
🔥 🔥 🔥 Open Source Airtable Alternative - turns any MySQL, Postgres, SQLite into a Spreadsheet with REST APIs.
Stars: ✭ 28,894 (+47267.21%)
Mutual labels:  restful-api

Api Example

以API接口开发为例,探索开发的最佳实践

分支列表

分支名称 说明
api-result-sample api-result结合Spring Boot使用实例

默认启用端口

9191

功能

  • 包结构规划最佳实践

  • API接口返回结果封装最佳解决方案

  • 使用Swagger实现漂亮、简洁、大方API接口文档

  • Swagger添加请求头

  • RESTful API规范接口实例(2020.4.12)

  • Swagger添加接口认证

  • MySQL数据库操作实例

  • MongoDB操作实例

  • Redis操作实例

  • Kafka操作实例

  • RabbitMQ操作实例

  • Elasticsearch操作实例

项目目录结构

APIExample
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── fengwenyi
│   │   │           └── api_example
│   │   │               ├── bean
│   │   │               │   └── PageResultDataBean
│   │   │               ├── config
│   │   │               │   └── Swagger2Config
│   │   │               ├── controller
│   │   │               │   ├── ApiTestResultController
│   │   │               │   └── ApiUserController
│   │   │               ├── data
│   │   │               │   └── UserData
│   │   │               ├── entity
│   │   │               │   └── UserEntity
│   │   │               ├── exceptions
│   │   │               │   └── DataParseException
│   │   │               ├── handler
│   │   │               │   └── GlobalExceptionHandler
│   │   │               ├── util
│   │   │               │   ├── ApiResultUtils
│   │   │               │   ├── PageResultUtils
│   │   │               │   └── ResultUtils
│   │   │               ├── vo
│   │   │               │   └── param
│   │   │               │       ├── UserFilterParamVO
│   │   │               │       └── UserParamVO
│   │   │               └── ApiExampleApplication
│   │   └── resource
│   │       ├── application.yml
│   │       └── banner.txt
│   └── test
│       └── java
│           └── com
│               └── fengwenyi
│                   └── api_example
│                       └── ApiExampleApplicationTests
└── pom.xml

controller

business

service

依赖

默认错误页面

<dependency>
    <groupId>com.github.iutil</groupId>
    <artifactId>app-boot-starter</artifactId>
    <version>${app-boot-starter.version}</version>
</dependency>

API接口结果处理解决方案

<dependency>
    <groupId>com.github.iutil</groupId>
    <artifactId>api-result</artifactId>
    <version>${api-result.version}</version>
</dependency>

常用工具类

<dependency>
    <groupId>com.github.iutil</groupId>
    <artifactId>JavaLib</artifactId>
    <version>${JavaLib.version}</version>
</dependency>

接口文档:Swagger

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

测试接口示例

控制台打印

[2019-06-05 10:17:02.516]  api-test -> get
[2019-06-05 10:17:12.900]  api-test -> add
[2019-06-05 10:17:24.037]  api-test -> update
[2019-06-05 10:17:36.409]  api-test -> delete

包结构

controller -> business -> service -> dbBusiness -> dbService

关于DB

dbService 只能由 dbBusiness调用

在 dbService 里处理所有与数据库相关的异常

查询由 find 开头
    携带All:查询所有的数据
    携带Use:查询没有被逻辑删除,且已经发布的数据
    携带Normal:查询没有被逻辑删除的数据

RESTful API

HTTP动词

对于资源的具体操作类型,由HTTP动词表示。

常用的HTTP动词有下面五个(括号里是对应的SQL命令)。

GET(SELECT):从服务器取出资源(一项或多项)。
POST(CREATE):在服务器新建一个资源。
PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。
PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)。
DELETE(DELETE):从服务器删除资源。

还有两个不常用的HTTP动词。

HEAD:获取资源的元数据。
OPTIONS:获取信息,关于资源的哪些属性是客户端可以改变的。

下面是一些例子。

GET /zoos:列出所有动物园
POST /zoos:新建一个动物园
GET /zoos/ID:获取某个指定动物园的信息
PUT /zoos/ID:更新某个指定动物园的信息(提供该动物园的全部信息)
PATCH /zoos/ID:更新某个指定动物园的信息(提供该动物园的部分信息)
DELETE /zoos/ID:删除某个动物园
GET /zoos/ID/animals:列出某个指定动物园的所有动物
DELETE /zoos/ID/animals/ID:删除某个指定动物园的指定动物

过滤信息(Filtering)

如果记录数量很多,服务器不可能都将它们返回给用户。API应该提供参数,过滤返回结果。

下面是一些常见的参数。

?limit=10:指定返回记录的数量
?offset=10:指定返回记录的开始位置。
?page=2&per_page=100:指定第几页,以及每页的记录数。
?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序。
?animal_type_id=1:指定筛选条件

参数的设计允许存在冗余,即允许API路径和URL参数偶尔有重复。比如,GET /zoo/ID/animalsGET /animals?zoo_id=ID 的含义是相同的。

错误处理

如果状态码是4xx,就应该向用户返回出错信息。一般来说,返回的信息中将error作为键名,出错信息作为键值即可。

{
    error: "Invalid API key"
}

返回结果

针对不同操作,服务器向用户返回的结果应该符合以下规范。

GET /collection:返回资源对象的列表(数组)
GET /collection/resource:返回单个资源对象
POST /collection:返回新生成的资源对象
PUT /collection/resource:返回完整的资源对象
PATCH /collection/resource:返回完整的资源对象
DELETE /collection/resource:返回一个空文档

2020.5.11 规划

api-example
    api-web
    api-webfluex
    api-swagger

版本

1.0.0 2021.01.21-

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].