进阶SpringBoot之配置 Swagger API 框架信息

Swagger:API 框架

RestFul API 文档在线自动生成工具,API 文档与 API 定义同步更新

Swagger 官网

Maven 仓库

创建 Spring Boot 项目,依赖勾选 Spring Web

pom.xml 导入相关依赖:

springfox-swagger2、springfox-swagger-ui

XML 复制代码
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

controller 包下新建 HelloController.java,先让项目跑起来

java 复制代码
package com.demo.swagger.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

http://localhost:8080/hello 启动成功即可,项目搭建成功

配置 Swagger:

(先把 Spring Boot 的版本降低到 2.5.5,

之前尝试过添加依赖 springfox-boot-starter、springdoc-openapi-starter-webmvc-ui,

配置类换注解 @EnableOpenApi、@EnableWebMvc,

Swagger 依赖到最新版本 3.0.0,一旦写了配置类,通通无效!根本无法启动,一个劲报错)

config 包下 SwaggerConfig.java 开启 Swagger2,@EnableSwagger2

java 复制代码
package com.demo.swagger.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
    
}

启动,进入 http://localhost:8080/swagger-ui.html

Swagger 给我们提供了一个前端页面

(新版地址换成了 http://localhost:8080/swagger-ui/index.html)

SwaggerConfig.java 配置类:(建议分析源码,点进 Docket 和 ApiInfo 查看)

配置 Swagger 的 Docket 的 bean 实例

配置 Swagger 信息 apiInfo

java 复制代码
package com.demo.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;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

@Configuration
@EnableSwagger2 //开启Swagger2
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("name","url","email");
        return new ApiInfo("Api Documentation",
                "Api Documentation",
                "1.0",
                "urn:tos",
                DEFAULT_CONTACT,   //contact
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        ); //从ApiInfo源码获取,可以任意修改,展示的前端页面就会显示修改后的页面
    }
}
相关推荐
麦兜*43 分钟前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
有没有没有重复的名字8 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
张先shen8 小时前
Elasticsearch RESTful API入门:基础搜索与查询DSL
大数据·spring boot·elasticsearch·搜索引擎·全文检索·restful
慕木兮人可9 小时前
Docker部署MySQL镜像
spring boot·后端·mysql·docker·ecs服务器