博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot之springfox(Swagger) (ApiDoc接口文档)
阅读量:5097 次
发布时间:2019-06-13

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

Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller的方法以文档的形式展现,基于Swagger。

官网地址:

1.maven依赖

io.springfox
springfox-swagger2
2.4.0
io.springfox
springfox-swagger-ui
2.4.0

 

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的标签就行了,哈哈哈哈

转载于:https://www.cnblogs.com/chen110xi/p/6197317.html

你可能感兴趣的文章
hdu 2526
查看>>
那些常用的git工具
查看>>
join()方法之我见
查看>>
希尔shell排序——java实现
查看>>
webService学习1----WSDL
查看>>
评估分类器性能的度量,像混淆矩阵、ROC、AUC等
查看>>
Scala - Spark Lambda“goesto“ => 分析
查看>>
mysql TIMESTAMPDIFF
查看>>
win7下docker环境搭建nginx+php-fpm+easyswoole+lavarel+mysql开发环境
查看>>
通过cmd查看环境变量名对应的环境变量值
查看>>
Python: 利用Python进行数据分析 学习记录
查看>>
python 零基础学习之路-06 常用模块
查看>>
[Lintcode]165. Merge Two Sorted Lists/[Leetcode]21. Merge Two Sorted Lists
查看>>
【ASP.NET 进阶】TreeView控件学习
查看>>
linux nfs配置
查看>>
【.Net Core】Assets file project.assets.json not found. Run a NuGet package restore
查看>>
mybatis框架
查看>>
编程语言
查看>>
自己的ORMapping
查看>>
读取NfcA格式数据
查看>>