栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集成Swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集成Swagger

一,引入Swagger3依赖
       
            io.springfox
            springfox-boot-starter
            3.0.0
        

这里不使用Swagger2的依赖了,使用Swagger2的依赖,需要两个dependency。
还要配置相应的propertis,可参考:https://blog.csdn.net/baidu_41909837/article/details/121573831

二,在启动类加EnableWebMvc

三,添加SwaggerConfig,添加配置类。

每部分的作用在下面代码注释中。

package com.seckill.seckilldemo.config;

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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)          //指定Api类型为Swagger2
                .apiInfo(apiInfo())                             //指定文档汇总信息
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.seckill.seckilldemo.controller")
                        ) //指定controller包路径
                .paths(PathSelectors.any())                     //指定展示所有controller
                .build();
    }

    private ApiInfo apiInfo(){
        //返回一个apiinfo
        return new ApiInfoBuilder()
                .title("Api接口文档")                                       //文档页标题
                .contact(
                        new Contact(
                                "Jackwong",
                                "https://blog.csdn.net",
                                "2537617411@qq.com")
                )                                                           // 联系人信息,姓名,邮箱。
                .description("api文档")                                       // 详细信息
                .version("1.0.1")                                           // 文档版本号
                .termsOfServiceUrl("https://www.baidu.com")                  //网站地址
                .build();
    }
}

测试:

1,写一个Controller:

2,启动Springboot启动类。
3,访问http://localhost:8080/swagger-ui/index.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号