摘要

springfox swagger展示restful的api doc, swagger is A POWERFUL INTERFACE TO YOUR API.

新增文件:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.boot.bind.RelaxedPropertyResolver;
  4. import org.springframework.context.EnvironmentAware;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.env.Environment;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.util.StopWatch;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import static springfox.documentation.builders.PathSelectors.regex;
  15. /**
  16. * api doc -- springfox swagger configuration
  17. */
  18. @Configuration
  19. @EnableSwagger2
  20. public class SwaggerConfiguration implements EnvironmentAware {
  21. private final Logger log = LoggerFactory.getLogger(SwaggerConfiguration.class);
  22. public static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";
  23. private RelaxedPropertyResolver propertyResolver;
  24. @Override
  25. public void setEnvironment(Environment environment) {
  26. this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
  27. }
  28. @Bean
  29. public Docket swaggerSpringfoxDocket() {
  30. log.debug("Starting Swagger");
  31. StopWatch watch = new StopWatch();
  32. watch.start();
  33. Docket swaggerSpringMvcPlugin = new Docket(DocumentationType.SWAGGER_2)
  34. .apiInfo(apiInfo())
  35. .genericModelSubstitutes(ResponseEntity.class)
  36. .select()
  37. .paths(regex(DEFAULT_INCLUDE_PATTERN)) // and by paths
  38. .build();
  39. watch.stop();
  40. log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
  41. return swaggerSpringMvcPlugin;
  42. }
  43. private ApiInfo apiInfo() {
  44. return new ApiInfo(
  45. propertyResolver.getProperty("title"),
  46. propertyResolver.getProperty("description"),
  47. propertyResolver.getProperty("version"),
  48. propertyResolver.getProperty("termsOfServiceUrl"),
  49. propertyResolver.getProperty("contact"),
  50. propertyResolver.getProperty("license"),
  51. propertyResolver.getProperty("licenseUrl")
  52. );
  53. }
  54. }

其中的DEFAULT_INCLUDE_PATTERN要适合你自己的接口格式。

用到的Maven依赖:

  1. <springfox.version>2.2.2</springfox.version>
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger-ui</artifactId>
  5. <version>${springfox.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger2</artifactId>
  10. <version>${springfox.version}</version>
  11. </dependency>

访问下面网址即可看到效果:

  1. http://localhost:8080/swagger-ui.html

另外,可以通过iframe插入到自己的网页中:

  1. <iframe src="swagger-ui.html" frameborder="0" marginheight="0" marginwidth="0"
  2. width="100%" height="900" scrolling="auto" target='_top'></iframe>

http://my.oschina.net/letao/blog/517852

spring boot之使用springfox swagger展示restful的api doc的更多相关文章

  1. Spring boot 多模块项目 + Swagger 让你的API可视化

    Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...

  2. 15、Spring Boot 2.x 集成 Swagger UI

    1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...

  3. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  4. Spring boot中使用springfox来生成Swagger Specification小结

    Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api   json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...

  5. Spring boot 、mybatis、swagger、c3p0、redis 和mongodb 整合

    文件路径:            添加依赖: <?xml version="1.0" encoding="UTF-8"?> <project ...

  6. Spring Boot 、mybatis 、swagger 和 c3p0 整合

    文件路径如下 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. Spring boot 、mybatis 和 swagger 整合

    文件路径 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  8. Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第102篇原创 03:WebFlux Web CR ...

  9. Spring Boot 中 10 行代码构建 RESTful 风格应用

    RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...

随机推荐

  1. cxSplitter.HotZone 怎么给分隔条增加值

    请使用它的类名指定HotZoneClassName . cxSplitter1.ResizeUpdate := True; cxSplitter1.HotZoneClassName := 'TcxMe ...

  2. Lambda 表达式的示例-来源(MSDN)

    本文演示如何在你的程序中使用 lambda 表达式. 有关 lambda 表达式的概述,请参阅 C++ 中的 Lambda 表达式. 有关 lambda 表达式结构的详细信息,请参阅 Lambda 表 ...

  3. c语言结构体数组定义的三种方式

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; int dangdang;//成员名可以和类名同名 }ddd[];/ ...

  4. ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4

    先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...

  5. jQuery鼠标悬停图片放大显示

    jQuery鼠标悬浮放于图片上之后图片放大显示的效果,即鼠标移到图片上图片突出显示,鼠标移开后恢复原来的模样,你可以在图片滚动效果中加上本特效,相信会更炫一些. <!DOCTYPE html P ...

  6. linux的chattr和lsattr命令

    chattr是用来改变文件.目录属性.chattr命令的作用很大,其中一些功能是由linux内核版本来支持的.通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录.lsattr是 ...

  7. 浅谈GitLab与Git

    前言:先解释下关于库的认识. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新增项目(远程仓库) 在GitLa ...

  8. ssh命令

    使用ssh命令登陆远程系统 ssh [ip/address] -l [登陆用户名] 如: ssh www.xyz.cn -l root

  9. exec与xargs区别

    区别描述: 两者都是对符合条件的文件执行所给的Linux 命令,而不询问用户是否需要执行该命令. -exec:{}表示命令的参数即为所找到的文件,以:表示comman命令的结束.\是转义符,因为分号在 ...

  10. 如何让div横向排列

    方法一: 一般情况,默认的div是写一个换一行,那么如何定义两个div横向排列而不换行呢? div默认的display属性是block.所以每一个div都是新的一行,现在把display换成inlin ...