博客
关于我
swagger入门教程
阅读量:343 次
发布时间:2019-03-04

本文共 3687 字,大约阅读时间需要 12 分钟。

1、添加pom文件

io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2

2、添加swagger的配置文件SwaggerDocumentationConfig:

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.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;@Configurationpublic class SwaggerDocumentationConfig {   	@Bean	public Docket prefixImplementation() {   		return new Docket(DocumentationType.SWAGGER_2).groupName("basic")				.select().paths(PathSelectors.ant("/basic/**")).build()				.apiInfo(apiInfo());	}		ApiInfo apiInfo() {   		return new ApiInfoBuilder().title("Swagger2 - API文档神器")				.title("XXXXXXX")				.description("RESTful风格的Web服务框架, 前后端分离后的契约, 在线API接口测试")				.license("查看druid监控:")				.licenseUrl("/v1/druid/login.html")				.termsOfServiceUrl("/v1/druid/login.html").version("2.0")				.version("1.0")				.build();	}}

上面basic对应于下图下拉框中的basic

在这里插入图片描述

3、在启动类中添加注解:

@EnableSwagger2

4、在Controller类中添加如下代码

@Api(value = "测试Controller操作集")@RestControllerpublic class TestController {       @ApiOperation(value = "发送短信验证码", notes = "", response = Map.class, tags = {   "basic.api"})    @ApiResponses(value = {               @ApiResponse(code = 200, message = "操作成功", response = Map.class),            @ApiResponse(code = 400, message = "输入参数非法", response = Map.class),            @ApiResponse(code = 404, message = "该记录不存在", response = Map.class),            @ApiResponse(code = 422, message = "操作失败", response = String.class)})    @RequestMapping(value = "/basic/api/sendsmsCode", method = RequestMethod.GET)//GET单参数传参请求    public void sendsmsCode(@ApiParam(value = "手机号", required = true) @RequestParam(value = "mobile", required = true) String mobile) {       }    @ApiOperation(value = "发送短信验证码", notes = "", response = Map.class, tags = {   "basic.api"})    @ApiResponses(value = {               @ApiResponse(code = 200, message = "操作成功", response = Map.class),            @ApiResponse(code = 400, message = "输入参数非法", response = Map.class),            @ApiResponse(code = 404, message = "该记录不存在", response = Map.class),            @ApiResponse(code = 422, message = "操作失败", response = String.class)})    @RequestMapping(value = "/basic/api/sendsmsCode1", method = RequestMethod.GET)//GET多参数传参请求    public void sendsmsCode1(@ApiParam(value = "手机号", required = true) @RequestParam(value = "mobile", required = true) String mobile,                             @ApiParam(value = "验证码类型", required = true) @RequestParam(value = "type", required = true) String type) {       }    @ApiOperation(value = "发送短信验证码", notes = "", response = Map.class, tags = {   "basic.api"})    @ApiResponses(value = {               @ApiResponse(code = 200, message = "操作成功", response = Map.class),            @ApiResponse(code = 400, message = "输入参数非法", response = Map.class),            @ApiResponse(code = 404, message = "该记录不存在", response = Map.class),            @ApiResponse(code = 422, message = "操作失败", response = String.class)})    @RequestMapping(value = "/basic/api/sendsmsCode2", method = RequestMethod.POST)//POST传参请求    public void sendsmsCode2(@ApiParam(value = "手机信息", required = true) @RequestBody String sendsmsCodeData) {       }}

接口生成工作原理:

1、系统启动,首先通过启动类中@EnableSwagger2注解扫描到api工程中的Swagger2Configuration类

2、然后通过该配置类中prefixImplementation方法扫描路径/basic/**,找到basic此包下及子包下标记有@Api注解的ControllerApi接口
3、根据改接口中的Swagger注解生成接口文档

转载地址:http://ryse.baihongyu.com/

你可能感兴趣的文章
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Slider滑杆和Numeric数值输入组件的使用
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用exec节点实现调用外部exe程序
查看>>
Node-RED中使用function函式节点实现数值计算(相加计算)
查看>>
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>