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. Python进阶-II 参数陷阱、命名空间、嵌套、作用域、闭包

    一.参数陷阱 在使用默认参数时,可能碰见下列情况 def show_args_trap(i, li = []): li.append(100) li[i] = 101 print(li) show_a ...

  2. OpenCV 学习笔记(14)为轮廓创建边界旋转框和椭圆

    https://docs.opencv.org/3.4/de/d62/tutorial_bounding_rotated_ellipses.html 不旋转 #include "opencv ...

  3. selenium--操作JS弹框

    前戏 我们常见的弹框有三种,一种是alert弹框,一种是prompt弹框,还有一种是confirm弹框那他们有什么不同呢?不同点就是他们长的不一样,alert弹框有一段文字和一个确定按钮,如下 在来看 ...

  4. 深入js系列-类型(开篇)

    思考 作为一个编程人员,你可能从来没仔细思考过,为什么这么多高级语言会有类型这东西. 实际上,类型有点类似生活中的类别,我们日常生活,早已经把这个概念理解到了,切肉和切水果会用不同的刀. 语言级别的类 ...

  5. shell 字符串拼接

    #!/bin/bash name="Shell" url="http://c.biancheng.net/shell/" str1=$name$url #中间不 ...

  6. SCDM导入点数据

    我们有时候需要把外部的点导入SCDM当中,但是SCDM没有像ICEM或者DM那样直接提供点导入的选项,是不是SCDM就无法导入点的数据了呢?答案当然是否定的.把点导入SCDM中的方法总结如下(示例数据 ...

  7. [Gamma] 项目展示

    [Gamma] 项目展示 一.工程展示 1.项目简介 定位分析 我们的目标是做一个创意分享网站,在之前的阶段中完成了大框架的搭建,并以此为基础进行界面优化与功能扩展. 典型用户 用户 面临困境 需求功 ...

  8. 终于有人把elasticsearch原理讲通了

    转自 小史是一个非科班的程序员,虽然学的是电子专业,但是通过自己的努力成功通过了面试,现在要开始迎接新生活了. 随着央视诗词大会的热播,小史开始对诗词感兴趣,最喜欢的就是飞花令的环节. 但是由于小史很 ...

  9. 【Activiti学习之八】Spring整合Activiti

    环境 JDK 1.8 MySQL 5.6 Tomcat 8 idea activiti 5.22 activiti-explorer是官方提供的一个演示项目,可以使用页面管理Activiti流程.ac ...

  10. Docker是什么?可以用Docker做什么

    其实可以把Docker理解成一个专门为应用程序与执行环境的轻型虚拟机. Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来.并且各种各样的货物被集装箱标准化了 ...