Dubbo 03 Restful风格的API
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文档可以作为:
- 需求和系统特性描述的根据
- 前后台查询、讨论、自测的基础
- 部分或者全部代码自动生成的根据
- 其他重要的作用,比如开放平台开发者的手册...
资源
官网
在线编辑器
编写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的更多相关文章
- PHP实现RESTful风格的API实例(三)
接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...
- PHP实现RESTful风格的API实例(二)
接前一篇PHP实现RESTful风格的API实例(一) Response.php :包含一个Request类,即输出类.根据接收到的Content-Type,将Request类返回的数组拼接成对应的格 ...
- PHP实现RESTful风格的API实例(一)
最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...
- PHP实现Restful风格的API
Restful是一种设计风格而不是标准,比如一个接口原本是这样的: http://www1.qixoo.com/user/view/id/1表示获取id为1的用户信息,如果使用Restful风格,可以 ...
- restful风格的API
在说restful风格的API之前,我们要先了解什么是rest.什么是restful.最后才是restful风格的API! PS(REST:是一组架构约束条件和原则,REST是Roy Thomes F ...
- [01] 浅谈RESTful风格的API
1.什么是RESTful风格的API REST,即Representational State Transfer,可以理解为"(资源的)表现层状态转化". 在网络上,我们通过浏览器 ...
- Gin实战:Gin+Mysql简单的Restful风格的API(二)
上一篇介绍了Gin+Mysql简单的Restful风格的API,但代码放在一个文件中,还不属于restful风格,接下来将进行进一步的封装. 目录结构 ☁ gin_restful2 tree . ├─ ...
- Gin实战:Gin+Mysql简单的Restful风格的API
我们已经了解了Golang的Gin框架.对于Webservice服务,restful风格几乎一统天下.Gin也天然的支持restful.下面就使用gin写一个简单的服务,麻雀虽小,五脏俱全.我们先以一 ...
- springMvc中restful风格的api路径中把小数点当参数,SpringMvc中url有小数点
在springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题, 有两种解决方案: 1:在api路径中加入:.+ @RequestMapping(&q ...
随机推荐
- spring boot configuration annotation processor not found in classpath
<dependency> <groupId> org.springframework.boot </groupId> <artifactId> spri ...
- Pytorch-创建tensor
引言 本篇介绍创建tensor的几种方式 Import from numpy from_numpy() float64 是 double 类型,也就是说从numpy导入的float其实是double类 ...
- maven的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- delete file SVN commit error has no URL
在提交SVN的时候遇到这个提交失败的提示: delete file SVN commit error has no URL 我的提交顺序是: 先在自己工程的文件夹删除 ->工程中删除 -> ...
- Qt qss 动态属性-不同条件不同显示
一. 1.为了用户界面外观的动态变化,属性选择器可以与动态属性组合使用. 2.当一个属性值变化时,所引用的样式不会自动更新.相反地,必须手动触发更新才会生效.unpolish()用于清理之前的样式,而 ...
- 【HANA系列】SAP HANA SQL查找字符串位置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL查找字符 ...
- A = min(1, max(0, A))
Crop A into [0, 1]:
- XML模块,面向对象思想与类的定义
今日内容 XML模块,面向对象思想,类的定义 1.XML模块 xml是一种可扩展的标记语言格式如下 使用 <> 作为标签格式 <tag style: '' color:read '' ...
- linux下使用URLOS搭建nextcloud私有云盘系统
Nextcloud是一个免费专业的私有云存储网盘开源项目,可以让你简单快速地在个人/公司电脑.服务器甚至是树莓派等设备上架设一套属于自己或团队专属的云同步网盘,从而实现跨平台跨设备文件同步.共享.版本 ...
- 配置传统vlan间路由
S1#SH RUN spanning-tree mode pvst ! interface FastEthernet0/1 switchport access vlan 10 switchport m ...