SpringBoot项目集成Swagger

本文介绍了如何在SpringBoot项目中集成Swagger2,包括在POM文件中添加依赖,配置相关类,以及启动后访问Swagger的文档页面。还提到了使用第三方UI替代官方UI的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的内容为:在SpringBoot项目中集成Swagger2,并且更换第三方UI

目录

在项目的POM文件中引入需要的资源GAV

增加配置类

启动项目,访问文档


项目的POM文件中引入需要的资源GAV

        <!-- swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
		</dependency>
        <!-- swagger2官方UI -->        
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>
		<!-- 第三方UI -->
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>swagger-bootstrap-ui</artifactId>
			<version>1.6</version>
		</dependency>

增加配置类

package com.imooc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;

/**
 * @description: Swagger2配置类
 * @author: WhiteCrowZHZ
 * @date: 2022/2/16 7:52
 */

@Configuration
@EnableSwagger2
public class Swagger2 {

    /**
     * 配置swagger2核心配置
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
                .apiInfo(apiInfo()) // 定义api文档汇总信息
                .select().apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) // 指定需要提供文档的Controller类所在的包
                .paths(PathSelectors.any()) // 需要生成文档的接口路径
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("这是接口文档页的标题")
                .contact(new Contact(
                        "name",
                        "http://example.com",
                        "example@qq.com"))
                .description("这是一段关于接口文档的描述信息")
                .version("1.0.1")
                .termsOfServiceUrl("http://example.com")
                .build();
    }
}

启动项目,访问文档

// 官方ui访问地址
http://localhost:8088/swagger-ui.html 
// 第三方ui访问地址
http://localhost:8088/doc.html

访问效果

官方UI:

 第三方UI:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值