springboot 配置 swagger2

发布时间:2022-06-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了springboot 配置 swagger2脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.pom.xml 添加依赖

        <!--swagger2 依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

2.配置一个配置类

springboot 配置 swagger2

 

 

 

springboot 配置 swagger2

springboot 配置 swagger2

package cn.cenxi.express.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
//@EnableSwagger2  [如果在这里配置的话,则不需要在启动类配置了]
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("校园快递代拿api")
                .description("校园快递代拿后端应用接口")
                .version("1.0")
                .build();
    }

}
View Code

3.在启动类开启

springboot 配置 swagger2

 

 

 

@EnableSwagger2

4.测试

启动工程后 ,

使用本地工程地址和端口 加 /swagger-ui.html

如我的 http://localhost:57/swagger-ui.html

springboot 配置 swagger2

 

 5.注解使用如下

springboot 配置 swagger2

 

 

springboot 配置 swagger2

 

 

springboot 配置 swagger2

 

脚本宝典总结

以上是脚本宝典为你收集整理的springboot 配置 swagger2全部内容,希望文章能够帮你解决springboot 配置 swagger2所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: