博客
关于我
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/

你可能感兴趣的文章
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理+负载均衡
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 多端口配置和访问异常问题的排查与优化
查看>>
Nginx 如何代理转发传递真实 ip 地址?
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的 SSL 模块安装
查看>>
Nginx 的优化思路,并解析网站防盗链
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>