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. mysql之子查询、视图、事务及pymysql等

    数据准备 CREATE TABLE `emp` ( `id` int(0) NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, `gender` ...

  2. ubuntu1604系统初始化

    1.初始化网络配置 1.1.创建工作目录 生产环境下必须有个固定的目录存放一些安装软件和调试工具, 否则每个管理员都随意存放软件工具,服务器的环境可以想而知 mkdir -p /opt/{tools, ...

  3. mysql命令查询语句&MTdata

    1.单表查询 select * from student; 采用*效率低,不推荐,多用列名 一.单表查询的语法: SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY ...

  4. js面向对象杂谈

    **万丈高楼平地起** 1. 通过命名规范创建具有私有属性的对象: 以__开头的对象为私有对象,但是实际是能访问到的. 2. 通过自执行函数中,return出来一个对象,return旁边的地方都可以写 ...

  5. c# Aes加解密

    using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; ...

  6. 一本通 1615:【例 1】序列的第 k 个数

    传送门 我在这里! 思路 输入一个序列的前三个数并求出这个序列的第K项,这个数列不是等比序列就是等差数列,等差数列比较好判断,如果序列中\(a_{i+2}-a_{i+1}=a_{i+1}-a_{i}\ ...

  7. springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)

    本文基于方志朋先生的博客实现:https://blog.csdn.net/forezp/article/details/70233227 一.准本工作 1.工具:Idea,JDK1.8,Maven3. ...

  8. 20165214 2018-2019-2 《网络对抗技术》Exp9 Web安全基础 Week13

    <网络对抗技术>Exp9 Web安全基础 Week13 一.实验目标与内容 1.实践内容 (1).本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目.包括(SQL,XSS,CSR ...

  9. 【Gamma】 Phylab 发布说明

    Phylab Gamma阶段发布说明 一.发布地址 网站:Phylab GitHub Release: WhatAHardChoice/Phylab Gamma版本 二.新功能 1. 控制台完全接入 ...

  10. @RequestMapping 用法详解

    简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...