SpringBoot之springfox(Swagger) (ApiDoc接口文档)
Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller的方法以文档的形式展现,基于Swagger。
官网地址:http://springfox.github.io/springfox/
1.maven依赖
<!--springfox-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
2.配置
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
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
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public Docket testApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test")
.genericModelSubstitutes(DeferredResult.class)
//.genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping("/test")//api测试请求地址
.select()
.paths(PathSelectors.regex("/common/.*"))//过滤的接口
.build()
.apiInfo(testApiInfo());
}
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("demo")
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/comm.*"))//过滤的接口
.build()
.apiInfo(demoApiInfo());
}
private ApiInfo testApiInfo() {
Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "2251181679@qq.com");
ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
"REST风格API",//小标题
"0.1",//版本
"www.baidu.com",
contact,//作者
"主页",//链接显示文字
""//网站链接
);
return apiInfo;
}
private ApiInfo demoApiInfo() {
Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "2251181679@qq.com");
ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
"REST风格API",//小标题
"0.1",//版本
"www.baidu.com",
contact,//作者
"主页",//链接显示文字
""//网站链接
);
return apiInfo;
}
}
3.restful注解描述
package com.example.demo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
/**
* Created by 王念 on 2016/4/25.
*/
@RestController
@RequestMapping("comm/")
public class Controller {
/**
* 简单接口描述
*
* @param userName
* @return
*/
@RequestMapping(value = "/demo1", method = RequestMethod.POST)
@ApiOperation(value = "测试接口1", notes = "简单接口描述 userName必填", code = 200, produces = "application/json")
public ModelMap getDemo(@RequestParam("userName") String userName) {
ModelMap map = new ModelMap();
map.addAttribute("userName", userName);
return map;
}
/**
* 参数描述
*
* @param userName
* @return
*/
@RequestMapping(value = "/demo2/{userName}", method = RequestMethod.POST)
@ApiOperation(value = "测试接口2", notes = "参数描述", code = 200, produces = "application/json")
public ModelMap getDemo2(@ApiParam(name = "userName", value = "编号", required = true) @PathVariable("userName") String userName) {
ModelMap map = new ModelMap();
map.addAttribute("userName", userName);
return map;
}
/**
* 接受对象
*
* @return
*/
@RequestMapping(value = "/demo3", method = RequestMethod.POST)
@ApiOperation(value = "测试接口3", notes = "接受对象", code = 200, produces = "application/json")
public ModelMap getDemoa(@RequestBody User user) {
ModelMap map = new ModelMap();
map.addAttribute("result", user);
return map;
}
@ApiIgnore //使用这个注解忽略这个接口
@RequestMapping(value = "/demo4", method = RequestMethod.POST)
public ModelMap getDemob(@RequestParam String content) {
ModelMap map = new ModelMap();
map.addAttribute("result", new java.util.Date());
return map;
}
}
访问http://localhost:8080/swagger-ui.html 就能看到
notes里 换行用 </br> HMLT的标签就行了,哈哈哈哈
http://my.oschina.net/wangnian/blog/666017
SpringBoot之springfox(Swagger) (ApiDoc接口文档)的更多相关文章
- Spring Boot 集成 Swagger 构建接口文档
在应用开发过程中经常需要对其他应用或者客户端提供 RESTful API 接口,尤其是在版本快速迭代的开发过程中,修改接口的同时还需要同步修改对应的接口文档,这使我们总是做着重复的工作,并且如果忘记修 ...
- asp.net core 使用 swagger 生成接口文档
参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...
- webapi 利用webapiHelp和swagger生成接口文档
webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...
- .net core 使用 swagger 生成接口文档
微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...
- springboot+mybatis-puls利用swagger构建api文档
项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...
- SpringBoot 使用Swagger2打造在线接口文档(附汉化教程)
原文地址: https://www.jianshu.com/p/7e543f0f0bd8 SpringBoot + Swagger2 UI界面-汉化教程 1.默认的英文界面UI 想必很多小伙伴都曾经使 ...
- spring boot 2.x 系列——spring-boot 集成 Swagger2 打造在线接口文档
文章目录 一.Springfox 与 Swagger 简介 1.1 Springfox 1.2 Swagger 1.3 OpenApi.Swagger.Springfox的关系 二.spring bo ...
- SpringBoot + Swagger2 自动生成API接口文档
spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...
- spring-boot-route(五)整合Swagger生成接口文档
目前,大多数公司都采用了前后端分离的开发模式,为了解决前后端人员的沟通问题,后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档,swagger2提供了强大的页面调试功能,这样可以有 ...
随机推荐
- git push (第一次) (转)
原地址 http://blog.csdn.net/kazeik/article/details/9113891 下图是github在创建仓库后给的提示:按它一步步操作下去就可以了. 下图是在git命 ...
- stage simulator
---恢复内容开始--- 运行自带地图 rosrun stage_ros stageros /opt/ros/indigo/share/stage_ros/world/willow-erratic.w ...
- [转]Web3.0时代,企业知识管理新趋势
[转自http://www.amt.com.cn/html/ManageFront/AMTPoint0/2014/0716/1370.html] Web3.0时代,企业知识管理新趋势 2014-07- ...
- hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce th ...
- OC中intValue要注意的地方
在程序中,发现一个问题,写了个例子,如下: NSDictionary * dict = [[NSDictionary alloc] init]; NSString * s ...
- GCD下的几种实现同步的方式
GCD多线程下,实现线程同步的方式有如下几种: 1.串行队列 2.并行队列 3.分组 4.信号量 实例: 去网上获取一张图片并展示在视图上. 实现这个需求,可以拆分成两个任务,一个是去网上获取图片,一 ...
- c语言基础
- Android Framework层Power键关机流程(二,关机流程)
二,关机流程 从前一篇博文我们知道,当用户长按Power键时会弹出(关机.重启,飞行模式等选项)对话框,我们点击关机,则会弹出关机确认对话框.那么从选项对话框到关机确认对话框又是一个什么流程呢.下面我 ...
- ZeroMQ - 三种模型的python实现
ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. 1.Request- ...
- C语言共用体内存计算
其实union(共用体)的各个成员是以同一个地址开始存放的,每一个时刻只可以存储一个成员,这样就要求它在分配内存单元时候要满足两点: 1.一般而言,共用体类型实际占用存储空间为其最长的成员所占的存储空 ...