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()
        );
    }
}
相关推荐
weisian15111 分钟前
Java并发编程--48-美团Leaf与百度UidGenerator:分布式ID生成器的工业级实践
java·leaf号段模式·leaf雪花模式·uidgenerator
胖纳特13 分钟前
Nextcloud 文件预览困局与破局:集成 BaseMetas Fileview 实现全格式在线预览
前端·后端
lczllx13 分钟前
MIT 6.824-lab3A(实现思路)
后端
派星13 分钟前
AOP实践:公共字段自动填充
后端
郝开15 分钟前
Spring Cloud Gateway 3.5.14 使用手册
java·数据库·spring boot·gateway
用户05101225729616 分钟前
从 malloc 到 DMA-BUF:嵌入式边缘设备上 mmap 的缓存一致性真相
后端
lczllx17 分钟前
MIT 6.824 MapReduce(实现思路)
后端
lczllx17 分钟前
C语言实现三子棋(详细讲解+源码)
后端
techdashen17 分钟前
Cloudflare 用 Rust 实现 QUIC 协议:quiche 是怎么设计的
开发语言·后端·rust
摇滚侠19 分钟前
IDEA 中快捷键的使用和修改 IDEA 中如何调试程序
java·ide·intellij-idea