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()
        );
    }
}
相关推荐
@大迁世界16 分钟前
相信我兄弟:Cloudflare Rust 的 .unwrap() 方法在 330 多个数据中心引发了恐慌
开发语言·后端·rust
vx_dmxq21124 分钟前
【PHP考研互助系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·mysql·考研·微信小程序·小程序·php
5***g29827 分钟前
新手如何快速搭建一个Springboot项目
java·spring boot·后端
daidaidaiyu1 小时前
Jetlinks 物联网平台 开源版学习源码分析
java·mqtt·rxjava·tcp
j***78881 小时前
clickhouse-介绍、安装、数据类型、sql
java
2***B4492 小时前
Rust在系统编程中的内存安全
开发语言·后端·rust
U***e632 小时前
Rust错误处理最佳实践
开发语言·后端·rust
q***47182 小时前
Spring中的IOC详解
java·后端·spring
vx_vxbs662 小时前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea
SunnyDays10113 小时前
如何使用 Java 删除 Word 文档中的水印
java·删除word文档水印