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提供了强大的页面调试功能,这样可以有 ...
随机推荐
- Octopus系列之js公共函数
货币选择 ChangeCurrency(this.value) 示例 <select name="currency" id="sl_currency" c ...
- java中Commons-fileupload实现上传
java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文 ...
- jQuery.extend源码深层分析
在网站的开发中,经常会自己写一些jQuery插件来方便使用,其中自然少不了一个关键的方法->jQuery.extend(),使用这个方法来扩展jQuery对象. 那么今天就来讲讲这个函数的实现原 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- UVA 208 (DFS)
题意:找出1到T的所有路径: 坑点:一开始以为是到终点,读错了题意,没测试第二个样例,结果WA了4遍,坑大了: #include <iostream> #include <cmath ...
- MEDIA-SYSSERVICES媒体播放
1 简单的音乐播放器 1.1 问题 本案例结合之前所学的网络和数据解析等知识完成一个网络音乐播放器,如图-1所示: 图-1 1.2 方案 首先创建一个SingleViewApplication应用,在 ...
- Array.prototype.indexOf
arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()
- iar 数据类型 int folat
8位或者16位的MCU,int占2字节32位的ARM是4字节 则430 int为2字节,则最大值32767,最小值-32768.
- 使用STL离散化
把原来的数组a复制一份拷贝b 用sort先把数组a排序 用unique消除a里面重复的元素 对于b中的每一个元素,用lower_bound找到它在a中的位置,也就是离散化之后的编号. 没了. #inc ...
- 以ls命令为实例介绍命令基本格式
登陆Linux命令行会显示一行字符,例如[root@localhost ~ ]#, 其中root表示当前登陆用户,localhost表示主机名,~显示的是当前路径,(-表示当前用户的家目录),#表示 ...