配置Swagger信息

一、配置Swagger

沿用上一篇博客项目

SwaggerConfig类

package com.massimo.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger
public class SwaggerConfig {

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

    //配置Swagger信息  ==== 》apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("马西莫", "https://www.baidu.com/", "2222222@qq.com");
        return new ApiInfo(
                "Massimo的Swagger文档",
                "你好,马西莫",
                "v1.0",
                "https://www.baidu.com/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }
}

二、测试效果

在这里插入图片描述

配置 Swagger 以实现 API 文档的自动生成和展示时,具体步骤会根据使用的框架有所不同。以下分别介绍在 Flask、Django 和 Spring Boot 中如何配置 Swagger。 ### Flask 配置 Swagger 在 Flask 框架中,可以使用 `Flask-Swagger` 或 `Swagger UI` 来生成和展示 API 文档。首先需要安装必要的库: ```bash pip install flask-swagger-ui ``` 然后在应用中配置 Swagger UI,并将 API 的元数据(如 JSON 格式的文档描述)提供给它: ```python from flask import Flask from flask_swagger_ui import get_swaggerui_blueprint app = Flask(__name__) # Swagger 文档的访问路径 SWAGGER_URL = '/swagger' # API 文档的位置(通常是一个 JSON 文件) API_URL = '/static/swagger.json' # 创建 Swagger UI 蓝图 swaggerui_blueprint = get_swaggerui_blueprint( SWAGGER_URL, API_URL, config={ 'app_name': "Flask API" } ) app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL) @app.route('/') def index(): return "Welcome to the Flask API!" if __name__ == '__main__': app.run(debug=True) ``` 确保有一个 `swagger.json` 文件放置在项目的 `static` 目录下,该文件可以通过工具如 `flask-swagger` 自动生成[^1]。 ### Django 配置 Swagger 对于 Django 项目,特别是使用 Django REST Framework (DRF) 构建的 API,推荐使用 `drf-yasg` 库来生成 OpenAPI 文档并集成 Swagger UI: 安装依赖包: ```bash pip install drf-yasg ``` 在 `settings.py` 中添加 `drf_yasg` 到 `INSTALLED_APPS`: ```python INSTALLED_APPS = [ ... 'rest_framework', 'drf_yasg', ] ``` 接着,在 `urls.py` 中配置 Swagger 的视图和路由: ```python from django.urls import path from drf_yasg.views import get_schema_view from dr7.openapi import Info schema_view = get_schema_view( openapi.Info( title="Your Project API", default_version='v1', description="Test description", ), public=True, ) urlpatterns = [ path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ] ``` 启动服务器后,通过访问 `/swagger/` 或 `/redoc/` 即可查看和测试 API 接口[^3]。 ### Spring Boot 配置 Swagger Spring Boot 使用 `springfox` 或者更新的 `springdoc-openapi` 来集成 Swagger。这里展示的是使用 `springdoc-openapi` 的基本配置: 添加 Maven 依赖到 `pom.xml`: ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.6.9</version> </dependency> ``` 如果需要控制线上环境是否启用文档,可以在 `application.properties` 设置如下参数: ```properties springdoc.api-docs.enabled=false ``` 这样在线上环境中就不会暴露 API 文档。开发或测试环境下设为 `true` 即可显示文档。 默认情况下,可以通过访问 `http://localhost:8080/swagger-ui.html` 查看交互式文档界面[^2]。 以上是针对不同框架配置 Swagger 实现 API 文档自动生成功能的基本方法。每种方案都提供了在线文档浏览与接口测试的能力,同时也支持进一步定制化需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值