swagger生成文档初步使用
在大部分情况下,公司都会要求提供详细的接口文档,对于开发来说,文档有时候在赶进度的情况下,也是一件头疼的事.而swagger的自动生成文档功能,就可以帮助我们减少工作量,对于文档的修改也可以在代码中随时修改,对于项目比较急的项目也不失为一种好的解决方案.
对于springboot项目,需要引入相关依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.</version>
</dependency> <dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.</version>
</dependency>
相关依赖为我们提供了可视化文档界面,并且支持文档的导出.
我们也需要加入相关的配置,spring boot一般推荐使用注解配置的形式,不推荐使用xml形式,所以在此添加config配置类.
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
public void setSwagger_is_enable(String swagger_is_enable){
enable=true; //此处为控制文档主页显示的开关 可以通过配置传入动态控制 }
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enable)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jzfq.swagger"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格,http://blog.csdn.net/saytime")
.version("1.0")
.build();
}
在此配置类里面,我们增加了ui界面的一些主页信息,在Bean的配置中,需要添加文档注解的扫描路径,这样我们的注解才能够被扫描解析到
在spring boot的启动类上开启swagger的自动配置 使用注解@EnableSwagger2
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2
@SpringBootApplication
public class Swagger01Application { public static void main(String[] args) {
SpringApplication.run(Swagger01Application.class, args);
}
}
由此spring boot就集成了swagger2.接下来我们可以一个demo测试接口文档的生成.
我们的参数可能直接使用vo来接收,我们可以创建一个User实体类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable; @ApiModel(description = "用户实体类")
public class User implements Serializable {
@ApiModelProperty(value ="姓名",allowableValues = "张三,李四,王五")
private String name;
@ApiModelProperty(value ="性别",allowableValues = "男,女,未知")
private String sex; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}
接下来创建一个controller层 通过post man进行调用
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class SwaggerTestController { @ApiOperation(value = "测试生成Swagger", notes = "测试生成Swagger", tags = "swagger模块")
@ApiImplicitParams(
@ApiImplicitParam(name = "age", value = "年龄", paramType = "body", dataType = "Integer", required = true)
)
@ApiResponses(
@ApiResponse(code = , message = "请求参数没填好")
)
@PostMapping("/test")
public void test( User user){
System.out.println("Spring boot 集成 Swagger2");
}
}
最后一步,我们通过访问 http://localhost:8080/doc.html地址,我们就可以看到这样一个界面,由此就集成了一个简单的swagger.对于swagger的注解还有很多,有兴趣的可以搜索一下注解的各种用法,满足我们的日常开发.

swagger生成文档初步使用的更多相关文章
- ASP.NET Core 1.0 中使用 Swagger 生成文档
github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...
- 使用swagger在netcorewebapi项目中自动生成文档
一.背景 随着前后端分离模式大行其道,我们需要将后端接口撰写成文档提供给前端,前端可以查看我们的接口,并测试,提高我们的开发效率,减少无效的沟通.在此情况下,通过代码自动生成文档,这种需求应运而生,s ...
- 使用DocFX生成文档
使用DocFX命令行生成文档 使用docfx 命令 1.下载 https://github.com/dotnet/docfx/releases 2.使用 创建初始项目 docfx init -q 此命 ...
- API Studio 5.1.2 版本更新:加入全局搜索、支持批量测试API测试用例、读取代码注解生成文档支持Github与码云等
最近在EOLINKER的开发任务繁重,许久在博客园没有更新产品动态了,经过这些日子,EOLINKER又有了长足的进步,增加了更多易用的功能,比如加入全局搜索.支持批量测试API测试用例.读取代码注解生 ...
- SpringBoot 集成Swagger2自动生成文档和导出成静态文件
目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...
- 使用Ldoc给Lua生成文档
Ldoc介绍 LDoc是一个Lua的文档生成工具,过去,比较常用的Lua生成文档的工具是LuaDoc,可惜作者自从2008年之后就再也没有发布过新的版本了,说明作者基本上已经放弃维护了.而LDoc则是 ...
- 使用PhpDocumentor生成文档
一,网站根目录执行 $ composer require --dev phpdocumentor/phpdocumentor 二,进入vendor/bin/目录执行 $phpdoc -d D:\ser ...
- doxygen的使用(一)配置并生成文档
原创文章,欢迎阅读,禁止转载. doxygen是个好用的文档生成工具,他的强大功能有很多介绍,我就不说了.自带的chm帮助手册很全面,包括功能.注释规范.怎么配置.工具用法等.doxygen的用法共3 ...
随机推荐
- Acwing-275-传纸条(DP)
链接: https://www.acwing.com/problem/content/description/277/ 题意: 给定一个 N*M 的矩阵A,每个格子中有一个整数. 现在需要找到两条从左 ...
- TextEdit不能空验证设置
在工具箱中找到dxValidationProvider控件拖拉到界面上,添加代码 ConditionValidationRule notEmptyValidationRule = new Condit ...
- 3DES加解密类
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace GT.C ...
- 【leetcode】1271. Hexspeak
题目如下: A decimal number can be converted to its Hexspeak representation by first converting it to an ...
- 【leetcode】1268. Search Suggestions System
题目如下: Given an array of strings products and a string searchWord. We want to design a system that su ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Find Integer
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- codevs 2602 最短路径问题x
题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线.若有连线,则表示 ...
- js模拟24小时的倒计时效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaWeb_响应和请求数据包
Google浏览器 F12中Network->Header部分 <%@ page language="java" import="java.util.*&qu ...