摘要

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

新增文件:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * api doc -- springfox swagger configuration
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration implements EnvironmentAware {
    private final Logger log = LoggerFactory.getLogger(SwaggerConfiguration.class);
    public static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";
    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
    }

    @Bean
    public Docket swaggerSpringfoxDocket() {
        log.debug("Starting Swagger");
        StopWatch watch = new StopWatch();
        watch.start();
        Docket swaggerSpringMvcPlugin = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .genericModelSubstitutes(ResponseEntity.class)
                .select()
                .paths(regex(DEFAULT_INCLUDE_PATTERN)) // and by paths
                .build();
        watch.stop();
        log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
        return swaggerSpringMvcPlugin;
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                propertyResolver.getProperty("title"),
                propertyResolver.getProperty("description"),
                propertyResolver.getProperty("version"),
                propertyResolver.getProperty("termsOfServiceUrl"),
                propertyResolver.getProperty("contact"),
                propertyResolver.getProperty("license"),
                propertyResolver.getProperty("licenseUrl")
        );
    }
}

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

用到的Maven依赖:

<springfox.version>2.2.2</springfox.version>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${springfox.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${springfox.version}</version>
</dependency>

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

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

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

<iframe src="swagger-ui.html" frameborder="0" marginheight="0" marginwidth="0"
        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. leetcode_question_119 Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  2. codility上的问题 (19)Sigma 2012

    题目: 像最大直方图一样给定一个数组是每个单位长度上的高度,求至少几个矩形可以拼出这个形状. 例如:给出的数组 H[0] = 8 H[1] = 8 H[2] = 5 H[3] = 7 H[4] = 9 ...

  3. C/C++捕获段错误,打印出错的具体位置(精确到哪一行)

    修订:2013-02-16 其实还可以使用 glibc 的 backtrace_symbols 函数,把栈帧各返回地址里面的数字地址翻译成符号描述的 修订:2011-06-11 背景知识: · 在li ...

  4. delphi中通过http控件上载文件的问题(紧急) 整理的CSDN 帖子

    http控件能不能实现post文件?要求效果就像普通的html中通过表单(form中<INPUT TYPE="FILE" NAME="FILE1" SIZ ...

  5. 九度OJ 1437 To Fill or Not to Fill

    题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样.若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离. 思路:贪心算法 1>若下一加油站的价格更便宜,则只需走 ...

  6. virsh -c exs://ip/?no_verify=1 --readonly nodeinfo

  7. javaDay1 基础知识

    常用dos命令 •d: 回车  盘符切换 •dir(directory):列出当前目录下的文件以及文件夹 •md (make directory) : 创建目录 •rd (remove directo ...

  8. linux shell在while中用read从键盘输入

    系统是ubuntu 14.04 64bit,之前曾想安装Stream来玩dota2,但最终没成功.由于Stream只有32bit,安装Stream时也安装了大量32bit的库.删除Stream后,这些 ...

  9. Java中判断集合类为空的方法

    *****需要引入Spring的核心Jar包***** 工具类: org.springframework.util.CollectionUtils 方法: public static boolean ...

  10. java -jdk配置1(环境变量配置)

    此文转载自:http://www.cnblogs.com/nicholas_f/articles/1494073.html 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1 ...