SpringBoot+swagger生成接口文档

springBoot+swagger生成接口文档

文章目录

目录

文章目录

一、接口文档是什么?

二、使用步骤

1.引入库

2.添加配置文件

3.添加配置信息

总结


一、接口文档是什么?

什么是接口文档?写接口信息的文档。
        每个接口的信息包括
        请求参数
        响应参数
        错误码
        接口地址
        接口名称
        请求类型
        请求格式
        备注

二、使用步骤

1.引入库

代码如下(示例):

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

2.添加配置文件

package com.yupi.xinggui.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Knife4j 接口文档配置
 * https://doc.xiaominfo.com/knife4j/documentation/get_start.html
 *
 * @author yupi
 */
@Configuration
@EnableSwagger2
@Profile("dev")
public class Knife4jConfig {

    @Bean
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("你的项目名称")
                        .description("你的项目名称")
                        .version("1.0")
                        .build())
                .select()
                // 指定 Controller 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.yupi.xinggui.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

3.添加配置信息

这一步很重要

如果你的springBoot版本>=2.6.0的话请添加一下配置

下方步骤很重要

//application格式
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

//yml格式
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

总结

今日时间2024年7月18日,希望该博客可以帮助到你,愿你开心每一天

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值