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()
        );
    }
}
相关推荐
华仔啊1 天前
千万别给数据库字段加默认值 null!真的会出问题
java·数据库·后端
IT_陈寒1 天前
别再死记硬背Python语法了!这5个思维模式让你代码量减半
前端·人工智能·后端
老赵全栈实战1 天前
【每日一技MyBatis trim标签核心用法
java·mybatis·orm
beata1 天前
Java基础-19:Java 死锁深度解析:从原理、检测到预防与实战指南
java·前端
xyy1231 天前
C# 读取 appsettings.json 配置指南
后端
code_YuJun1 天前
Spring ioc 完全注解
后端
kevinzeng1 天前
反射的初步理解
后端·面试
下次一定x1 天前
深度解析 Kratos 客户端服务发现与负载均衡:从 Dial 入口到 gRPC 全链路落地(上篇)
后端·go
kevinzeng1 天前
Spring 核心知识点:EnvironmentAware 接口详解
后端
xyy1231 天前
C# / ASP.NET Core 依赖注入 (DI) 核心知识点
后端