springboot整合swagger3.0

目录

一、介绍及优点

二、项目中使用swagger

2.1 环境准备

      1、新建:  springboot-web项目

      2、导入springboot整合swagger的依赖

 3、配置swagger

 3.1、配置application.yml

3.2、启动类添加@EnableOpenApi或@EnableSwagger2

3.3、自定义文档信息

3.4、给实体类、字段、前端控制器、Api接口添加文档注解

3.5、注解使用场景:

三、使用

写在最后【以下内容与正文关系不大】:


一、介绍及优点

  •   swagger是一个生成在线文档的API框架。
  •   可以使用生产的在线文档实时测试新增的接口。
  •   便于维护和前后端联调。

二、项目中使用swagger

需要两个组件的依赖:

  1. swagger
  2. swagger-ui

2.1 环境准备

springboot 2.7.0

swagger 3.0

      1、新建:  springboot-web项目

                传送门:使用IDEA新建一个springboot项目

      2、导入springboot整合swagger的依赖

     <!--swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

 3、配置swagger

        3.1配置application.yml、编写SwaggerConfiguration配置类、开注解。

         

 3.1、配置application.yml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

3.2、启动类添加@EnableOpenApi或@EnableSwagger2

@SpringBootApplication
@EnableOpenApi
//@EnableSwagger2
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

随后启动项目访问:http://localhost:8080/swagger-ui/index.html

 在线文档大概可以分为以上四个部分;若不想使用默认的信息,则需要编写SwaggerConfiguration配置类进行个性化设置。

3.3、自定义文档信息

        新建SwaggerConfiguration配置类,注册下Docket实例,使用源码里提供有参构造器来对文档页面自定义:

提供了以上可配置项、例如、SwaggerConfiguration配置类如下:

/**
 * @author Alex
 * @EnableSwagger2 开启swagger2
 */

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    /**
     * 配置Docket实例
     * @return
     */
    @Bean
    public Docket getDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(
                new ApiInfoBuilder()
                        .contact(new Contact("听钱塘信来","https://blog.csdn.net/chenyunjiangNN","123@qq.com"))
                        .title("Swagger在线文档")
                        .build()
                )
                .groupName("demo-project-123");
    }


}

效果:

 3.4、给实体类、字段、前端控制器、Api接口添加文档注解

 效果:

 

效果:

等等、总之对对于实体、字段、接口、查询参数添加描述、让前后端联调更方便。

3.5、注解使用场景:


@Api    对请求Controller的说明
@ApiOperation    方法的说明
@ApiImplicitParams    方法参数的说明;
@ApiImplicitParam    用于指定单个参数的说明。
@ApiResponses    方法返回值的说明 ;
@ApiResponse    用于指定单个参数的说明。
@ApiModel    用在JavaBean类进行说明
@ApiModelProperty    用在JavaBean类的属性进行说明

        三、使用

根据接口信息给对应的参数值、执行后查看相应内容和状态码;比如下图的根据条件查询用户

此文中获取全部用户的接口是基于mybatis作为持久层实现的、此处就不一一说明了;

mybatis相关传送门:

springboot整合mybatis、springboot整合mybatis-plus   

填写对应参数后执行:

过程中报的一个异常:

Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null

解决:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

写在最后【以下内容与正文关系不大】:

问: 为什么访问:http://localhost:8080/swagger-ui/index.html会进入在线文档的页面?

答:我们进入springboot整合swagger的依赖看一下,已经包括了swagger-ui 3.0,无需单独引入swagger -ui 3.0的依赖;

在看一下swagger-ui3.0的依赖,就会找到index.html页面

问:前后端分离项目中、使用swagger在线文档有需要注意的地方吗?

答:有时会因为缓存导致文档不能实时更新、会导致配置、字段、Api接口之类的会有差别。说到底swagger在线文档只是提供测试接口和前后端协作的一个方案而已。不用过于依赖。

问:除了swagger-ui,还有其他第三方实现的在线文档吗?

        <!--   swagger-bootstrap-ui -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

页面:访问http://localhost:8080/doc.html

 

问:我想通过其他请求访问到swagger的首页可以吗?

答:将swagger页面所需的静态资源导入项目中,再随便写个请求访问到index.html就可以了。若是静态资源404,将静态资源放行、且要保证引用路径正确。

 

 

问:可以动态选取swagger文档在指定环境才能生效吗?

答:在Docket实例中、读取指定配置文件、拿到环境信息、注入enable(false) 即可关闭;

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好!要将Spring Boot 2.6与Swagger 3.0整合在一起,你可以按照以下步骤进行操作: 步骤1:添加Swagger依赖 在你的Spring Boot项目的pom.xml文件中,添加Swagger的依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> </dependencies> ``` 步骤2:配置Swagger 创建一个Swagger配置类,用于配置Swagger的相关信息: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @Configuration @EnableSwagger2 public class SwaggerConfig { public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("API 文档") .version("1.0.0") .build(); } } ``` 步骤3:启用Swagger 在你的Spring Boot应用程序的主类上使用`@EnableSwagger2`注解来启用Swagger: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(SwaggerConfig.class) public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 步骤4:访问Swagger UI 在启动你的应用程序后,你可以通过访问`http://localhost:8080/swagger-ui/`来查看生成的API文档。 以上就是将Spring Boot 2.6与Swagger 3.0整合的基本步骤。你可以根据自己的需要进一步定制和配置Swagger。希望能对你有所帮助!如果有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值