SpringBoot集成Swagger2

记录了简单的在SpringBoot项目中加Swagger2工具,暂时没加Swagger2用法

pom.xml配置

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

Swagger2配置类

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;

/**
 * @author: zy
 * @date: 2021/7/13 12:19
 * @since JDK 1.8
 */
@Configuration
@EnableSwagger2
public class Swagger2Configure {
        @Bean
        public Docket createRestApi() {
        /*
        通过createRestApi创建docket
        apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。
        select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,
        扫描com.mx.controller包下面定义的方法作为展示的RestFul api,产生文档内容
         */
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.mx.controller"))
                    .paths(PathSelectors.any()).build();
        }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("XX平台API接口文档")
                //创建人及其网站与邮箱地址(可自行更换)
                .contact(new Contact("ZY", "http://localhost:8080/swagger-ui.html",
                        "zhenyu_li1998@163.com"))
                //版本号
                .version("1.0")
                //描述(可显示在swagger可视化界面
                .description("系统API描述")
                .build();
    }
}

记得在启动类加上注解

@EnableSwagger2

 

全部评论

相关推荐

07-17 12:07
门头沟学院 Java
勇敢牛牛不怕困难
投递OPPO等公司7个岗位
点赞 评论 收藏
分享
积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务