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()
        );
    }
}
相关推荐
Coder_Boy_1 分钟前
基于DDD+Spring Boot 3.2+LangChain4j构建企业级智能客服系统
java·人工智能·spring boot·后端
开心猴爷3 分钟前
苹果商店 App 上架要求,探讨如何通过系统审核
后端
开心就好20253 分钟前
Flutter 应用加固在真实项目中的实践方式,当 Dart 之外还有一整个 IPA
后端
黄俊懿9 分钟前
【深入理解SpringCloud微服务】Spring-Security作用与原理解析
java·后端·安全·spring·spring cloud·微服务·架构师
我是谁的程序员10 分钟前
iOS 开发中证书创建与管理中的常见问题
后端
塔能物联运维17 分钟前
设备自适应采样率忽视能耗致续航降 后来结合功耗模型动态调优
java·后端·struts
我是谁的程序员17 分钟前
iPhone 耗电异常检测的思路,从系统电池统计、克魔(KeyMob)、Instruments等工具出发
后端
掘金一周23 分钟前
前端一行代码生成数千页PDF,dompdf.js新增分页功能| 掘金一周 12.25
前端·javascript·后端
ServBay24 分钟前
掌握这9个GO技巧,代码高效又高能
后端·go
rchmin25 分钟前
Spring Boot自动装配原理解析
java·spring boot·后端