SpringBoot整合swagger2进行API文档编程

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括添加依赖、配置类编写、常用注解说明及访问路径。通过本教程,开发者可以快速实现API文档自动生成。

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

1. 添加依赖

		<!-- swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>

2. 添加配置类

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName parent
 * @title     SwaggerConfig   
 * @package    com.gdd.videos.common  
 * @date   2019/9/3 9:44  
 * @explain swagger2的配置内容仅仅就是需要创建一个Docket实例
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    // 创建一个Docket实例
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .apiInfo(apiInfo())
                .select()
                // controller接口路径
                .apis(RequestHandlerSelectors.basePackage("com.gdd.videos.mini_api.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboordemo")
                .description("Springboot整合Demo")
                .version("0.0.1")
                .build(); // 这部分信息其实可以自定义到配置文件中读取
    }

}

3. 常用注解:
附上链接: https://blog.csdn.net/ajklaclk/article/details/80736042

4. 启动后访问路径
http://localhost:8080/swagger-ui.html
页面如图

注意:
如果是多模块配置,还需在项目入口文件中加上要扫描的包基本路径:
如图:

在这里插入图片描述
谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老司机张师傅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值