SpringBoot项目整合Swagger

1.首先我们需要搭建一个Springboot项目,不会的可以看我的搭建教程

2.在pom文件当中导入依赖

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

3.当依赖导入成功之后,我们便可以开始下一步操作,在properties配置文件当中配置如下配置

#swagger
spring.mvc.pathmatch.matching-strategy: ant_path_matcher

4.最后编写我们的配置文件即可,配置文件名称:SwaggerConfig,代码如下

package com.zhang.config;

import com.google.common.base.Predicates;
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.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;

/**
 * swagger测试
 * 测试接口
 * @Author:爱做梦的小咸鱼
 * @Date: 2022/4/14  19:57
 */
@Configuration//标注为配置类
@EnableSwagger2//swagger注解
public class SwaggerConfig {
    /**
     * 配置swagger插件
     * @return
     */
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)  //类型
                .groupName("webApi")    //组名称
                .apiInfo(webApiInfo())//在这里注入到首页
                .select()
//                当接口中不包含(not)admin,error的时候将会显示到swagger中
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    /**
     * 网站的提示信息
     * @return
     */
    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("请关注爱做梦的小咸鱼")
                .description("相当多的学习资源,欢迎大家留言讨论")
                .version("1.0")
                .contact(new Contact("爱做梦的小咸鱼", "http://baidu.com", "谢谢"))
                .build();
    }
}

5.上述操作完成之后,我们便可以启动项目,通过地址来进行访问了,地址规则如下:

你的ip地址:项目端口号/swagger-ui.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱做梦的小咸鱼

对您提供帮助,是我的荣幸

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

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

打赏作者

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

抵扣说明:

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

余额充值