Swagger接口文档

1、导入swagger依赖

https://blog.csdn.net/xhmico/article/details/125353535?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166894302316782425113195%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166894302316782425113195&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~times_rank-5-125353535-null-null.142%5Ev65%5Ewechat,201%5Ev3%5Econtrol_1,213%5Ev2%5Et3_control2&utm_term=swagger&spm=1018.2226.3001.4187

<!-- swagger 接口文档 -->
<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>
​

2、然后在config中新建一个类,SwaggerConfig.class

image-20240122022505566

SwaggerConfig.class:

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;
​
/**
 * 
 * @ClassName: yupao-backend01
 * @Description: 自定义 Swagger 接口文档的配置
 */
@Configuration // 配置类
@EnableSwagger2 // 开启 swagger2 的自动配置
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
​
                // 配置接口信息
                .select() // 设置扫描接口
                // 配置如何扫描接口
                .apis(RequestHandlerSelectors
                                //.any() // 扫描全部的接口,默认
                                //.none() // 全部不扫描
                                .basePackage("com.kunyuan.usercenter.controller") // 扫描指定包下的接口,最为常用
                        //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                        //.withMethodAnnotation(PostMapping.class) // 扫描带有只当注解的方法接口
​
                )
                .paths(PathSelectors
                                .any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }
​
    // 基本信息设置
    private ApiInfo apiInfo() {
        Contact contact = new Contact(
                "哈哈", // 作者姓名
                "kunyuan.cn", // 作者网址
                "kunyuan@qq.com"); // 作者邮箱
        return new ApiInfoBuilder()
                .title("接口文档") // 标题
                .description("相信自己一定可以的") // 描述
                .termsOfServiceUrl("https://www.baidu.com") // 跳转连接
                .version("1.0") // 版本
                .license("Swagger的使用详细教程")
                .licenseUrl("https://www.baidu.com")
                .contact(contact)
                .build();
    }
​
}
​

3、解决报错

image-20240122022706357

解决方法:

 mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

image-20240122022828567

4、运行启动

http://localhost:8080/api/swagger-ui.html

  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值