RestFull api接口

  前后端分离开发的接口规范

  什么是RestFull 是目录比较流行的api设计规范

  注:restfull api规范应用场景,前后端分离的项目中

数据接口的现场 例如:

  /users/999 获取ID为999的信息

  /users/list  获取所有的用户信息

  /users/add  打开添加的页面

  /users/save 新增数据

  /users/edit 打开修改的页面

  /users/save 根据表单是否有主键的值判断是否有更新

  /users/del/999  删除id 为999的信息

Restfull风格的api接口,通过不同的请求方式来区分不同的操作

  get /users/999  获取id为999的信息

  get /users  获取所有的用户信息

  post /users 新增一条记录

  put /users 修改信息

  patch /users 增量的修改

  delete /users/999 删除id为999的信息

如何创建restfull风格的数据接口

  注:springmvc对restfull风格的api有很好的支持

风格如下

package com.seecen.sc1904springboot.controller;

import com.seecen.sc1904springboot.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList;
import java.util.List; /**
* get /users/999 获取id为999的信息
* get /users 获取所有的用户信息
* post /users 新增一条记录
* put /users 修改信息
* patch /users 增量的修改
* delete /users/999 删除id为999的信息
*/
@Controller
@RequestMapping("users")
public class UserController { //get users/9999
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public User getUserById(@PathVariable("id") Integer id) {
//持久化操作:根据id获取指定记录并返回
User user = new User();
user.setUserId(id);
user.setUserName("张三");
return user;
} //get /users 获取所有的用户信息
@RequestMapping(value = "",method = RequestMethod.GET)
@ResponseBody
public List<User> getAllUsers(){
List<User> list = new ArrayList<>();
User user = new User();
user.setUserId(1);
user.setUserName("张三");
list.add(user);
return list;
} // post /users 新增一条记录
@RequestMapping(value = "",method = RequestMethod.POST)
@ResponseBody
public User addNewUser(User user){
//新增一条记录并获取user对象
return user;
} // put /users 修改信息
@RequestMapping(value = "",method = RequestMethod.PUT)
@ResponseBody
public User updateUser(User user){
return user;
} //patch /users 增量的改
@RequestMapping(value = "",method = RequestMethod.PATCH)
@ResponseBody
public User patchUser(User user){
return user;
} //delete /users/999
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
@ResponseBody
public User del(@PathVariable("id") Integer id){
return new User();
}
}

Postman测试

  

  

Swaggerui框架测试

  1. 导入依赖包
  <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>

  2.编写配置文件(JAVA类的方式进行配置)

    Java类来管理bean对象

    通过@Bean注解来管理bean对象 (必须要配置)

package com.seecen.sc1904springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//@Configuration 就个类是一个spring框架的配置文件
//spring框架的配置文件主要体现的是创建什么bean对象
@Configuration//spring配置文件,xml, java类来体现配置信息
@EnableSwagger2
public class Swagger2 {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.seecen.sc1904springboot.controller"))
.paths(PathSelectors.any())
.build();
} /**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("Spring Boot中使用Swagger2构建RESTful APIs")
.termsOfServiceUrl("http://www.geek5.cn")
.contact(new Contact("calcyu","http://geek5.cn","hi@geek5.cn"))
.version("1.0")
.build();
}
}

 注解

    @Api:用在类上,说明该类的作用。

    @ApiOperation:注解来给API增加方法说明。

    @ApiImplicitParams : 用在方法上包含一组参数说明。

    @ApiImplicitParam:用来注解来给方法入参增加说明。

    @ApiResponses:用于表示一组响应

    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

    code:数字,例如400

  message:信息,例如"请求参数没填好"

   response:抛出异常的类

    @ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

    @ApiModelProperty:描述一个model的属性

    注意:@ApiImplicitParam的参数说明

控制层controller(上面Swagger2类中 这个包下所有控制层的方法都会获取

package com.seecen.sc1904springboot.controller;

import com.seecen.sc1904springboot.pojo.RESTfullResult;
import com.seecen.sc1904springboot.pojo.User; import com.seecen.sc1904springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController // 返回的所有都是json的
@RequestMapping("/user")
@Api("用户的增删改查功能")
public class UserController2 { @Autowired
private UserService userService; // emp/insert获取用户信息 @PathVariable("empno")Integer empno @GetMapping("/{id}")
@ApiOperation("根据id主键返回用户信息")
@ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "json")
public RESTfullResult<User> getAllUsers(@PathVariable("id")Integer id) {
User list = userService.selectByPrimaryKey(id);
return RESTfullResult.success(list);
} }
项目开始运行了  访问测试地址:http://项目实际地址/swagger-ui.html

然后就可以对控制层的所有方法进行测试了
   
测一个添加方法吧
    

执行后

执行成功了

数据库看看

ok

这就是Swaggerui框架测试

restFull api接口的更多相关文章

  1. nova client和nova restfull api区别

    1.nova client封装了获取认证 获取token等东西 2.nova  client提供shell cli方式访问和import client 然后new client的方式访问 区别: 通过 ...

  2. php 使用 restler 框架构建 restfull api

    php 使用 restler 框架构建 restfull api restler 轻量级,小巧,构建restfull api非常方便! 官网:http://restler3.luracast.com/ ...

  3. Restfull API 示例

    什么是Restfull API Restfull API 从字面就可以知道,他是rest式的接口,所以就要先了解什么是rest rest 不是一个技术,也不是一个协议 rest 指的是一组架构约束条件 ...

  4. 使用Jax-rs 开发RESTfull API 入门

    使用Jax-rs 开发RESTfull API 入门 本文使用 Jersey 2开发RESTfull API.Jersey 2 是 JAX-RS 接口的参考实现 使用到的工具 Eclipse Neon ...

  5. 小白的springboot之路(十一)、构建后台RESTfull API

    0.前言 开发系统中,前后端分离,后端一般返回RESTfull  API,前端调用API构建UI,彼此分离.互相完全独立: 后台API中,我们一般返回结果码.提示信息.数据三部分内容,如图: 我们今天 ...

  6. 干货来袭-整套完整安全的API接口解决方案

    在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...

  7. 12306官方火车票Api接口

    2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...

  8. 快递Api接口 & 微信公众号开发流程

    之前的文章,已经分析过快递Api接口可能被使用的需求及场景:今天呢,简单给大家介绍一下微信公众号中怎么来使用快递Api接口,来完成我们的需求和业务场景. 开发语言:Nodejs,其中用到了Neo4j图 ...

  9. web api接口同步和异步的问题

    一般来说,如果一个api 接口带上Task和 async 一般就算得上是异步api接口了. 如果我想使用异步api接口,一般的动机是我在我的方法里面可能使用Task.Run 进行异步的去处理一个耗时的 ...

随机推荐

  1. java读取HDFS压缩文件乱码

    java通过调用HDFS系统的FileSystem等API 直接读取HDFS的压缩文件会产生乱码 解决方法: 1.调用解码的API,解码后通过IO流处理. public static void mai ...

  2. [THUPC2019]过河卒二(组合数学,容斥原理)

    以后都懒得写题目大意和数据范围了. hz学长的题其实也不那么毒瘤吗.比CDW的好多了 先考虑没有障碍怎么做. 首先发现,答案相当于一个左下角是 $(1,1)$,右上角是 $(n+1,m+1)$ 的棋盘 ...

  3. 2017年计算语义相似度最新论文,击败了siamese lstm,非监督学习

    Page 1Published as a conference paper at ICLR 2017AS IMPLE BUT T OUGH - TO -B EAT B ASELINE FOR S EN ...

  4. [算法模板]SOS DP

    [算法模板]SOS DP 正文 SOS-DP(\(\text{Sum over Subsets}\))是用来解决这样的问题的: 其实就是子集和DP.上面每个\(F[mask]\)里面包含了\(mask ...

  5. ShowDoc 软件开发团队接口文档管理利器

    ShowDoc是一个非常适合IT团队的在线API文档.技术文档工具.你可以使用Showdoc来编写在线API文档.技术文档.数据字典.在线手册. 这里介绍 Showdoc 这款开源(免费)文档管理系统 ...

  6. 【VS开发】【C/C++开发】关于boost库的C++11导致的undefined符号问题

    undefined reference to boost::program_options::options_description::m_default_line_length 问题最终解决依靠的是 ...

  7. python 散点图上给每个点打标签方便看到数据

    import numpy as np import matplotlib.pyplot as plt x=[2.3,4.5,3,7,6.5,4,5.3] y=[5,4,7,5,5.3,5.5,6.2] ...

  8. 《Linux就该这么学》培训笔记_ch16_使用Squid部署代理缓存服务

    <Linux就该这么学>培训笔记_ch16_使用Squid部署代理缓存服务 文章最后会post上书本的笔记照片. 文章主要内容: 代理缓存服务 配置Squid服务程序 正向代理 标准正向代 ...

  9. 基于ZYNQ 的UART中断实验之串口写数据到DDR3中

    1.参考 UG585 网络笔记 2.理论知识 参见上一次实验:基于ZYNQ 的UART中断实验 3.实验目的 练习使用UART的中断实验,并将接收到的数据写入到DDR3中. 4.实验过程 建立工程,设 ...

  10. Typescript 学习 - 类

    class class 并不是一种新的数据结构,只是在函数原型基础上的语法糖 class People { hand: number; constructor(hand: number) { this ...