Dubbo03

restful风格的API

Representational State Transfer,资源表现层状态转换

根路径

mashibing.com

协议

http://

版本

v1

可以直接写在URL上,或者写在header中传递“Accept-Version: v2”

 @RequestMapping(headers = "Accept-Version=v2",value = "models",method = RequestMethod.GET)

用HTTP协议里的动词来实现资源的增删改查

GET 用来获取资源,
POST 用来新建资源(也可以用于更新资源)。
DELETE 用来删除资源。
UPDATE http://api.chesxs.com/v1/fence 更新围栏信息

用例

单个资源

http://mashibing.com/api/v1/Users/1 使用Get方法获取id是1的用户数据

         正确:GET /model/models/{id}        #获取单个资源

         正确:POST /model/models            #创建单个资源

         正确:PUT /model/models/{id}        #更新单个资源

         正确:DELETE /model/models/{id}   #删除单个资源

         正确:PATCH /model/models/{id}   #更新单个资源(只传差异)

         正确:GET /model/configRuleFile    #获取单个资源(如果仅有一个值时,应采用单数方式)

返回结果:

如果指定的资源并不存在,那么应该返回404 Not Found状态,否则应该返回200 OK状态码

资源集合

对于资源集合,支持以下URL

   正确: GET /model/models                             #获取资源列表

   正确: GET /model/models?ids={ids}             #批量获取资源列表

   正确: DELETE /model/models?ids={ids}       #批量删除资源列表

   返回结果:

如果列表为空,则应该空数组

响应结果

响应状态码 含义
成功 200 调用成功
201 创建成功
204 执行成功,但无返回值
失败 400 无效请求
401 没有登录
403 没有权限
404 请求的资源不存在
500 服务内部错误

swagger(丝袜哥)

Swagger是一个简单但功能强大的API表达工具。它具有地球上最大的API工具生态系统,数以千计的开发人员,使用几乎所有的现代编程语言,都在支持和使用Swagger。使用Swagger生成API,我们可以得到交互式文档,自动生成代码的SDK以及API的发现特性等。

OpenAPI

OpenAPI规范是Linux基金会的一个项目,试图通过定义一种用来描述API格式或API定义的语言,来规范RESTful服务开发过程。OpenAPI规范帮助我们描述一个API的基本信息

比如:

  • 有关该API的一般性描述
  • 可用路径(/资源)
  • 在每个路径上的可用操作(获取/提交...)
  • 每个操作的输入/输出格式

根据OpenAPI规范编写的二进制文本文件,能够像代码一样用任何VCS工具管理起来一旦编写完成,API文档可以作为:

  • 需求和系统特性描述的根据
  • 前后台查询、讨论、自测的基础
  • 部分或者全部代码自动生成的根据
  • 其他重要的作用,比如开放平台开发者的手册...

资源

官网

https://swagger.io/

在线编辑器

http://editor.swagger.io/

编写API文档

我们可以选择使用JSON或者YAML的语言格式来编写API文档

swagger: '2.0'
info:
version: 1.0.0
title: mashibing.com api
description: 马老师的官网接口
contact:
name: yiming
url: http://mashibing.com
email: 888@qqq.com
license:
name: MIT
url: http://opensource.org/licenses/MIT
schemes:
- http host: mashibing.com
basePath: /api/v1 paths:
/user/{userid}:
get:
summary: 获取一个用户
description: 根据id获取用户信息
parameters:
- name: userid
in: path
required: true
description: 用户id
type: string
responses:
200:
description: OK /user:
get:
summary: 返回List 包含所有用户
description: 返回List 包含所有用户
parameters:
- name: pageSize
in: query
description: 每页显示多少
type: integer
- name: pageNum
in: query
description: 当前第几页
type: integer responses:
200:
description: OK schema:
type: array
items:
required:
- username
properties:
username:
type: string
password:
type: string

整合SpringBoot

官方依赖

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>

第三方

https://github.com/SpringForAll/spring-boot-starter-swagger

依赖引入

<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>

启用注解

http://localhost:803//v2/api-docs

http://localhost:8080/swagger-ui.html

分组

swagger.docket.controller.title=group-controller
swagger.docket.controller.base-package=com.mashibing.springboot.controller swagger.docket.restcontroller.title=group-restcontroller
swagger.docket.restcontroller.base-package=com.mashibing.springboot.controller.rest

实体模型

	@ApiModelProperty(value = "权限id", name = "id",dataType = "integer",required = true,example = "1")
private Integer id;

接口方法

	@ApiOperation(value = "获取所有权限")
@RequestMapping(value = "list",method = RequestMethod.GET)
public List<Permission> list() { return permissionSrv.findAll();
} @ApiOperation(value = "添加权限")
@RequestMapping("update")
public RespStat update(@ApiParam(name="permission",required = true, example = "{json}",value = "权限对象") @RequestBody Permission permission) { System.out.println("permission:" + ToStringBuilder.reflectionToString(permission));
permissionSrv.update(permission);
return RespStat.build(200);
}

接口类描述

@Api(value = "用户权限管理",tags={"用户操作接口"})

Dubbo 03 Restful风格的API的更多相关文章

  1. PHP实现RESTful风格的API实例(三)

    接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...

  2. PHP实现RESTful风格的API实例(二)

    接前一篇PHP实现RESTful风格的API实例(一) Response.php :包含一个Request类,即输出类.根据接收到的Content-Type,将Request类返回的数组拼接成对应的格 ...

  3. PHP实现RESTful风格的API实例(一)

    最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...

  4. PHP实现Restful风格的API

    Restful是一种设计风格而不是标准,比如一个接口原本是这样的: http://www1.qixoo.com/user/view/id/1表示获取id为1的用户信息,如果使用Restful风格,可以 ...

  5. restful风格的API

    在说restful风格的API之前,我们要先了解什么是rest.什么是restful.最后才是restful风格的API! PS(REST:是一组架构约束条件和原则,REST是Roy Thomes F ...

  6. [01] 浅谈RESTful风格的API

    1.什么是RESTful风格的API REST,即Representational State Transfer,可以理解为"(资源的)表现层状态转化". 在网络上,我们通过浏览器 ...

  7. Gin实战:Gin+Mysql简单的Restful风格的API(二)

    上一篇介绍了Gin+Mysql简单的Restful风格的API,但代码放在一个文件中,还不属于restful风格,接下来将进行进一步的封装. 目录结构 ☁ gin_restful2 tree . ├─ ...

  8. Gin实战:Gin+Mysql简单的Restful风格的API

    我们已经了解了Golang的Gin框架.对于Webservice服务,restful风格几乎一统天下.Gin也天然的支持restful.下面就使用gin写一个简单的服务,麻雀虽小,五脏俱全.我们先以一 ...

  9. springMvc中restful风格的api路径中把小数点当参数,SpringMvc中url有小数点

    在springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题, 有两种解决方案: 1:在api路径中加入:.+ @RequestMapping(&q ...

随机推荐

  1. 监控系统-PMM

    Percona Monitoring and Management (PMM)是一款开源的用于管理和监控MySQL和MongoDB性能的开源平台 通过PMM客户端收集到的DB监控数据用第三方软件Gra ...

  2. Android逆向——破解水果大战

    最近公司需要测试安卓app安全,但安卓基本上0基础,决定开始学习下安卓逆向根据吾爱破解上教程 <教我兄弟学Android逆向系列课程+附件导航帖> https://www.52pojie. ...

  3. PTA --- 天梯赛 L1-064 估值一亿的AI核心代码

    L1-064 估值一亿的AI核心代码 (20 point(s)) 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多 ...

  4. 语音识别LD3320

    一.概述 1.芯片介绍 LD3320 是一颗基于非特定人语音识(SI-ASR:Speaker-Independent  Automatic Speech Recognition)技术的语音识/声控芯片 ...

  5. Python报错module 'scipy.misc' has no attribute 'xxx'

    Python报错module 'scipy.misc' has no attribute 'imresize' 解决办法: 安装Pillow包,命令如下: pip install Pillow 然后重 ...

  6. 【计算机视觉】【神经网络与深度学习】论文阅读笔记:You Only Look Once: Unified, Real-Time Object Detection

    尊重原创,转载请注明:http://blog.csdn.net/tangwei2014 这是继RCNN,fast-RCNN 和 faster-RCNN之后,rbg(Ross Girshick)大神挂名 ...

  7. [转帖]MySQL5.7.20编译安装

    MySQL5.7.20编译安装 尝试一下 想着 我在arm上面最终安装失败了. https://www.cnblogs.com/shengdimaya/p/8027507.html 1:官网下载sou ...

  8. SpringBoot自动化配置之四:@Conditional注解详解

    前言 之前在分析spring boot 源码时导出可见@ConditionalOnBean 之类的注解,那么它到底是如何使用的以及其工作流程如何,我们这里就围绕以下几点来分析: @Conditiona ...

  9. C/C++的几个输入流

    C: 1.scanf( ) 存在于<stdio.h>(C++为<cstdio>)中,根据stdin读取数据并根据参数格式进行赋值,以第一个非空格字符(空格字符如:空格,制符表, ...

  10. C++ 的关键字(保留字)完整介绍

    转载至:https://www.runoob.com/w3cnote/cpp-keyword-intro.html 1. asm asm (指令字符串):允许在 C++ 程序中嵌入汇编代码. 2. a ...