SpringBoot集成Swagger2

一、SpringBoot集成Swagger2

1.1、导入两个springfox的jar包

  • Springfox-Swagger2
  • Swagger-Springmvcs

二、使用Swagger

要求:jdk 1.8 + 否则Swagger2无法运行

步骤:

2.1、新建一个SpringBoot-web项目

2.2、添加Maven依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.9.2</version>
</dependency>

2.3、编写HelloController,测试确保运行成功!

2.4、要使用Swagger,我们需要编写一个配置类SwaggerConfig来配置 Swagger

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
     
}

2.5、访问测试 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面;

在这里插入图片描述

三、配置Swagger

3.1、Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。

@Configuration //定义Java配置类
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
   
    @Bean //配置docket以配置Swagger具体参数
    public Docket docket() {
   
       return new Docket(DocumentationType.SWAGGER_2);
    }
}

3.2、可以通过apiInfo()属性配置文档信息

//配置Swagger的apiInfo信息
    @Bean
    public ApiInfo apiInfo(){
   
        //作者信息
        Contact CONTACT = new Contact("未进化的程序猿", "https://www.cnblogs.com/the-undeveloped-procedural-ape/", "[email protected]");
        return new ApiInfo("未进化的程序猿的Swagger日志", //Swagger文档标题
                           "Api Documentation",//Swagger文档描述
                           "v1.0",//Swagger文档版本号
                           "https://www.cnblogs.com/the-undeveloped-procedural-ape/",//博客地址
                           CONTACT,//作者信息
                           "Apache 2.0",
                           "http://www.apache.org/licenses/LICENSE-2.0",
                           new ArrayList());
    }

3.3、Docket 实例关联上 apiInfo()

@Bean
public Docket docket() {
   
   return new Docket(DocumentationType.SWAGGER_2).apiInfo(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值