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. 五个goland进行go开发的小技巧

    五个goland进行go开发的小技巧 本文译自5 Tips To Speed Up Golang Development With IntelliJ Or Goland 确实很实用. 1. 实现int ...

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

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

  3. idea 2018.1 创建springboot开启找回Run Dashboard

    Run Dashboard 他是一个代替Run窗口的一个更好清晰简介的一个启动器.一般我们需要启动多个窗口时,Run窗口不能让我们直观的看到我们看到每一个端口的变化. 我们可以对比一下这个是Run D ...

  4. Java 并发系列之九:java 原子操作类Atomic(13个)

    1. 原子更新基本类型类 2. 原子更新数组 3. 原子更新引用 4. 原子更新属性 5. txt java 原子操作类Atomic 概述 java.util.concurrent.atomic里的原 ...

  5. nmap简单使用方法

    1.作用扫描整个网络的主机服务状态和存活优点,快速,准确,效率高2.nmap 选项 Usage: nmap [Scan Type(s)] [Options] {target specification ...

  6. Laravel自动备份到阿里云OSS

    背景 之前做备份时,主要是拿一台备份机对生产机做数据库做主备,用rsync同步上传的图片,文件.随着项目的增多,许多小项目都这样做感觉太过繁琐,每次都要在2台机器之间配置,同时单独拿一台机器做备份成本 ...

  7. docker 学习操作记录 2

    记录2 [BEGIN] // :: Connecting to ... Connection established. To escape to local shell, press Ctrl+Alt ...

  8. AVLMap平衡二叉树

    public class AVLMap<K, V> implements Iterable<AVLEntry<K, V>> { private int size; ...

  9. BitSet源码

    public class BitSet1 implements Cloneable, java.io.Serializable { // >>>左边补0, << 右边补0 ...

  10. 【转】用 async/await 来处理异步

    原文地址:https://www.cnblogs.com/SamWeb/p/8417940.html 昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简 ...