Swagger保姆级教学

Swagger 简介

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

快速上手

1.新建一个springboot项目

2.导入maven依赖(swagger2和swagger UI)

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

3.启动项目,访问http://localhost:端口/swagger-ui.html

4.新建一个SwaggerConfig.java

package com.littlepage.config;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
@EnableSwagger2
public class SwaggerConfig { /**
* 配置swagger bean实例
* @return
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
} /**
* 配置swagger信息
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfo("steve yu's API document",
"for quick start swagger", "1.0",
"https://www.cnblogs.com/littlepage/",
new Contact("steve yu", "https://www.cnblogs.com/littlepage/", "littlepageprogram@outlook.com"),
"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList<>());
}
}

启动页面

5.配置扫描接口(修改Bean)

@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).
apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.littlepage.controller")).build();
}

6.在环境中进行配置(工程扫描接口开关)

@Bean
public Docket docket(Environment environment){
Profiles profiles=Profiles.of("dev","test");//假设有dev和test环境变量
boolean flag=environment.acceptsProfiles(profiles);
return new Docket(DocuemtatuionType.SWAGGER_2).enable(flag);//如果环境配置了,则生效,否则不生效
}

7.API文档的分组。

new Docket().group(String s)
//这个String s可以规定分组

8.单API样例(参考https://www.jianshu.com/p/349e130e40d5)

#####Controller代码
@Override
@ApiOperation(value = "post请求调用示例", notes = "invokePost说明", httpMethod = "POST")
public FFResponseModel<DemoOutputDto> invokePost(@ApiParam(name="传入对象",value="传入json格式",required=true) @RequestBody @Valid DemoDto input) {
log.info("/testPost is called. input=" + input.toString());
return new FFResponseModel(Errcode.SUCCESS_CODE, Errcode.SUCCESS_MSG);
} #####接口请求入参对象
@Data
@ApiModel(value="演示类",description="请求参数类" )
public class DemoDto implements Serializable { private static final long serialVersionUID = 1L; @NotNull
@ApiModelProperty(value = "defaultStr",example="mockStrValue")
private String strDemo; @NotNull
@ApiModelProperty(example="1234343523",required = true)
private Long longNum; @NotNull
@ApiModelProperty(example="111111.111")
private Double doubleNum; @NotNull
@ApiModelProperty(example="2018-12-04T13:46:56.711Z")
private Date date; } #####接口请求出参公共类
@ApiModel(value="基础返回类",description="基础返回类")
public class FFResponseModel<T> implements Serializable { private static final long serialVersionUID = -2215304260629038881L;
// 状态码
@ApiModelProperty(example="成功")
private String code;
// 业务提示语
@ApiModelProperty(example="000000")
private String msg;
// 数据对象
private T data; ...
} #####接口请求出参实际数据对象
@Data
public class DemoOutputDto { private String res; @NotNull
@ApiModelProperty(value = "defaultOutputStr",example="mockOutputStrValue")
private String outputStrDemo; @NotNull
@ApiModelProperty(example="6666666",required = true)
private Long outputLongNum; @NotNull
@ApiModelProperty(example="88888.888")
private Double outputDoubleNum; @NotNull
@ApiModelProperty(example="2018-12-12T11:11:11.111Z")
private Date outputDate; }

Swagger保姆级教学的更多相关文章

  1. RabbitMQ从概念到使用、从Docker安装到RabbitMQ整合Springboot【1.5w字保姆级教学】

    @ 目录 一.前言 二.RabbitMQ作用 1. 异步处理 2. 应用解耦 3. 流量控制 三.RabbitMQ概念 1. RabbitMQ简介 2. 核心概念 四.JMS与AMQP比较 五.Rab ...

  2. 【保姆级教学】新手第一次搭建vue项目和初始化

    前端项目初始化步骤 安装vue脚手架 通过vue脚手架创建项目 配置vue路由 配置Element-UI组件库 配置axios库 初始化git远程仓库 将本地项目托管到github或者码云上 通过vu ...

  3. 保姆级别的RabbitMQ教程!一看就懂!(有安装教程,送安装需要的依赖包,送Java、Golang两种客户端教学Case)

    保姆级别的RabbitMQ教程!一看就懂!(有安装教程,送安装需要的依赖包,送Java.Golang两种客户端教学Case)   目录 什么是AMQP 和 JMS? 常见的MQ产品 安装RabbitM ...

  4. 保姆级教程——Ubuntu16.04 Server下深度学习环境搭建:安装CUDA8.0,cuDNN6.0,Bazel0.5.4,源码编译安装TensorFlow1.4.0(GPU版)

    写在前面 本文叙述了在Ubuntu16.04 Server下安装CUDA8.0,cuDNN6.0以及源码编译安装TensorFlow1.4.0(GPU版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...

  5. 自建本地服务器,自建Web服务器——保姆级教程!

    搭建本地服务器,Web服务器--保姆级教程! 本文首发于https://blog.chens.life/How-to-build-your-own-server.html. 先上图!大致思路就是如此. ...

  6. 重磅:保姆级Java技术图谱发布!够学到元宵节了,赶紧收藏!

    最近因为参与社群交流的时间比较多,除了唠唠白酒的嗑之外,很大一部分时间都是看到群里问到一些关于Spring Boot和Spring Cloud应用过程中碰到的问题以及一些开发过程中的报错信息.在这些帮 ...

  7. 【保姆级】利用Github搭建自己的个人博客,看完就会

    大家好,我是辰哥~ 作为一名喜欢技术的爱好者,平时喜欢把自己学习技术的心得或者一些踩坑.易错的过程记录下来,首选的是技术平台(博客),今天辰哥来教大家如何利用Github来搭建一个自己的个人博客平台. ...

  8. Mysql读写锁保姆级图文教程

    摘要:读锁会阻塞写,但是不会阻塞读,而写锁会把杜希俄都阻塞. 本文分享自华为云社区<Mysql保姆级读写锁图文教程丨[绽放吧!数据库]>,作者:Code皮皮虾 . 准备 创建mylock表 ...

  9. JavaWeb和WebGIS学习笔记(七)——MapGuide Open Source安装、配置以及MapGuide Maestro发布地图——超详细!目前最保姆级的MapGuide上手指南!

    JavaWeb和WebGIS学习笔记(七)--MapGuide Open Source安装.配置以及MapGuide Maestro发布地图 超详细!目前最保姆级的MapGuide上手指南! 系列链接 ...

随机推荐

  1. Ubuntu 16.04 RabbitMq 安装与运行

    前言目前公司用阿里云 + redis 的方式实现的消息队列.了解了目前几种主流的消息组件(主要包括rabbitmq.kafka.)的优缺点后,这里为了深入学习rabbitmq,我在自己的腾讯云服务器上 ...

  2. Unity学习笔记_控制人物移动+摄像机跟随

    我想做的移动操作方式类似[流星蝴蝶剑].[龙之谷].[我的世界第三人称]的第三人称操作方式. 操作说明:W键会朝当前镜头方向前进,鼠标控制镜头旋转. 做前需知(先去稍微了解一下比较好): ①unity ...

  3. noi openjudge7627:鸡蛋的硬度

    http://noi.openjudge.cn/ch0206/7627/ 描述 最近XX公司举办了一个奇怪的比赛:鸡蛋硬度之王争霸赛.参赛者是来自世界各地的母鸡,比赛的内容是看谁下的蛋最硬,更奇怪的是 ...

  4. 洛谷 题解 UVA1626 【括号序列 Brackets sequence】

    看还没有人发记搜的题解,赶紧来水发一篇 我们定义dp[i][j]为区间i~j内最少添加几个括号才能把这个串变成正规括号序列. 考虑四种情况 i>j不存在这种子串,返回0 i==j子串长度为1无论 ...

  5. 《MIT 6.828 Lab 1 Exercise 2》实验报告

    本实验链接:mit 6.828 lab1 Exercise2. 题目 Exercise 2. Use GDB's si (Step Instruction) command to trace into ...

  6. MySQL之基础认识与操作

    MySQL数据库 开发学习中,想满足一些需求,无疑需要经常与数据打交道,例如,我们在使用IO的一些技术的时候,常常需要将一些数据存储到外部文件,可能大家会问,我们初学的时候常常会简单的保存一些数据到 ...

  7. [转帖]centos7设置CPU的运行频率为performance

    centos7设置CPU的运行频率为performance http://www.512873.com/archives/612.html Publish: March 6, 2019 Categor ...

  8. [转帖]tr命令的用法

    tr命令的用法 https://www.cnblogs.com/bingguoguo/articles/9188703.html tr命令   tr命令可以对来自标准输入的字符进行替换.压缩和删除.它 ...

  9. IDEA插件之FindBugs

    1.是个啥? Findbugs,它是一个静态分析工具,用来查找Java代码中的程序错误.它使用静态分析来识别Java程序中上百种不同类型的潜在错误. 2.安装 File -> Settings ...

  10. Android试题

    1. Binder:例子: https://blog.csdn.net/qq_33208587/article/details/82767720