Swagger使用

Swagger

简介

  • 号称世界上最流行的API框架;
  • Restful API 文档在线生成工具 ---> API文档与API定义同步更新
  • 直接运行,可以在线测试 API 接口;
  • 支持各种语言;(Java,PHP....)

官网

Spring Boot 集成 Swagger

在项目中使用 Swagger 需要Springfox

  • swagger 2
  • swagger ui

1、新建一个Spring Boot = web 项目;

2、导入相关依赖

xml 复制代码
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

3、HelloWorld

4、配置 Swagger ==> Congfig

java 复制代码
@Configuration
@EnableSwagger2     // 开启 Swagger 2
public class SwaggerConfig {
}

5、访问页面:http://localhost:8080/swagger-ui.html

配置 Swagger

Swagger 的 bean实例 Docket;

java 复制代码
@Configuration
@EnableSwagger2     // 开启 Swagger 2
public class SwaggerConfig {

    // 配置了 Swagger 的 Docket 的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    // 配置 Swagger 信息
    public ApiInfo apiInfo(){

        // 作者信息
        Contact contact = new Contact("小贱", "http://sword-man.cn/index.html", "xiaojian2436@163.com");

        return new ApiInfo("小贱的Swagger API文档",
                "但行好事,莫问前程",
                "1.0",
                "http://sword-man.cn/index.html",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}
相关推荐
kakawzw9 分钟前
微服务组件源码2——Spring Ribbon原理(基于RibbonLoadBalancerClient)
java·微服务·ribbon
计算机安禾12 分钟前
【算法分析与设计】第48篇:流算法与数据概要技术
java·服务器·网络·数据库·算法
hunterkkk(c++)18 分钟前
SPFA最短路径算法(c++)
java·c++·算法
Java_2017_csdn37 分钟前
在 Java 中,MessageFormat.format() 和 String.format() 函数对比?
java·开发语言·前端·数据库
卷无止境1 小时前
C# 与 .NET 中的委托:把方法装进变量里
后端
绛洞花主敏明1 小时前
Go操作xorm中间表多对多关联实战
开发语言·后端·golang
长栎1 小时前
手写一个表达式计算器,你就理解解释器模式了
后端
噢,我明白了1 小时前
MyBatis-Plus 中IPage的分页查询
java·mybatis
长栎1 小时前
foreach 语法糖背后,迭代器模式做了多少脏活
后端
剑挑星河月1 小时前
98.验证二叉搜索树
java·算法·leetcode