当前位置:   article > 正文

springboot集成swagger 2.X/3.X的方法以及常见报错_springboot3.x支持swagger2吗

springboot3.x支持swagger2吗

纯小白一枚,因为前后端分离需要使用到api接口文档,所以才注意到swagger,之前我是完全不是知道这是干什么的。经过信息检索和上手实践后,出现各种问题,下面我将从如何集成到日常使用中容易出现的问题(ps:对于纯小白而言最容易出现的一些问题)

一、集成步骤及问题

集成步骤可以看这篇文章,写的真的很详细,不管swagger2.x还是3.x都有详细介绍:https://blog.csdn.net/lsqingfeng/article/details/123678701

总的来讲就是:

1.引入依赖(这里有一个注意的问题:如果你跟我一样配置完成之后一开始访问api文档控制台就报异常:

  1. Illegal DefaultValue null for parameter type integer
  2. NumberFormatException: For input string: ""

这是因为swagger自带的annotations和models依赖为1.5.20版本的,实体类使用@ApiModelProperty时,example属性没有赋值导致的,在AbstractSerializableParameter的getExample方法中会将数值属性的example的转换数值类返回,example的默认值是"",因此当example没有赋值时,会出现上面的异常。其源码路径为io.swagger.models.parameters.AbstractSerializableParameter.getExample()

(ps:可参考:https://blog.csdn.net/weixin_44299027/article/details/105810872)

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.9.2</version>
  5. <!--解决 Illegal DefaultValue null for parameter type integer 异常-->
  6. <!--1.排除swagger2的annotations和models依赖-->
  7. <exclusions>
  8. <exclusion>
  9. <groupId>io.swagger</groupId>
  10. <artifactId>swagger-annotations</artifactId>
  11. </exclusion>
  12. <exclusion>
  13. <groupId>io.swagger</groupId>
  14. <artifactId>swagger-models</artifactId>
  15. </exclusion>
  16. </exclusions>
  17. </dependency>
  18. <!--2.引入1.5.21以上版本的annotations和models依赖-->
  19. <dependency>
  20. <groupId>io.swagger</groupId>
  21. <artifactId>swagger-annotations</artifactId>
  22. <version>1.5.21</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>io.swagger</groupId>
  26. <artifactId>swagger-models</artifactId>
  27. <version>1.5.21</version>
  28. </dependency>
  29. <!-- swagger-ui增强(一般都会使用ui增强工具,swagger自带的ui贼丑而且不直观便捷) -->
  30. <dependency>
  31. <groupId>com.github.xiaoymin</groupId>
  32. <artifactId>swagger-bootstrap-ui</artifactId>
  33. <version>1.9.6</version>
  34. </dependency>

2.进行配置
首先需要添加一个注解 : @EnableSwagger2。 这个注解可以直接添加到SpringBoot的启动类上,也可以自定义一个配置类,放到上面。添加这个注解代表在项目中开启Swagger的功能。

一般都是自己定义一个配置类,正好还可以添加一个Docket配置。 可以自定义Docket配置,就是一组(一个项目或一个版本)接口文档的配置,比如设置名称, 联系人等等。

在config文件夹下,添加一个SwaggerConfig类

  1. package com.wc.demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.spring.web.plugins.Docket;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.PathSelectors;
  7. import springfox.documentation.builders.RequestHandlerSelectors;
  8. import springfox.documentation.service.ApiInfo;
  9. import springfox.documentation.service.Contact;
  10. import springfox.documentation.spi.DocumentationType;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. import org.springframework.core.env.Environment;
  13. import org.springframework.core.env.Profiles;
  14. import java.util.ArrayList;
  15. /**
  16. * @author WangChen 2064318526@qq.com
  17. * @date 2023/8/10 16:09
  18. */
  19. @Configuration
  20. @EnableSwagger2
  21. public class SwaggerConfig {
  22. @Bean
  23. public Docket createDocket(Environment environment){
  24. return new Docket(DocumentationType.SWAGGER_2)
  25. // 设置是否启动Swagger,默认为true(不写即可),关闭后Swagger就不生效了
  26. .enable(true)
  27. // 分组名称
  28. .groupName("Demo")
  29. // 文档信息配置
  30. .apiInfo(apiInfo())
  31. // 配置扫描的接口
  32. .select()
  33. // 配置扫描哪里的接口
  34. .apis(RequestHandlerSelectors.basePackage("com.wc.demo.controller"))
  35. // 过滤请求
  36. .paths(PathSelectors.any())
  37. .build();
  38. }
  39. private ApiInfo apiInfo(){
  40. return new ApiInfoBuilder()
  41. .title("WangChen接口文档")//文档标题
  42. .description("记录项目接口")//文档基本描述
  43. .contact(new Contact("wangchen", "https://blog.csdn.net/m0_68118199", "2064318526@qq.com"))//这里可以添加联系人信息
  44. .termsOfServiceUrl("http://terms.service.url/WangChen") // 组织链接
  45. .version("v1.0")//版本
  46. .license("Apache 2.0 许可") // 许可
  47. .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0") // 许可链接
  48. .extensions(new ArrayList<>()) // 拓展
  49. .build();
  50. }
  51. }

 3.yml文件添加以下内容:

  1. spring:
  2. mvc:
  3. pathmatch:
  4. matching-strategy: ANT_PATH_MATCHER

 4.之后就可以开始使用了,在控制层和类对象中使用注解进行说明就行。如果想仅在测试环境test,开发环境dev中使用api文档;yml中加入:

  1. spring:
  2. mvc:
  3. pathmatch:
  4. matching-strategy: ANT_PATH_MATCHER
  5. profiles:
  6. active: dev # 可换

配置类中的docket中改为这样:

  1. @Bean
  2. public Docket docket1(Environment environment){
  3. Profiles profiles = Profiles.of("dev", "test"); // 设置要显示swagger的环境
  4. boolean isOpen = environment.acceptsProfiles(profiles); // 判断当前是否处于该环境
  5. return new Docket(DocumentationType.SWAGGER_2)
  6. // 设置是否启动Swagger,默认为true(不写即可),关闭后Swagger就不生效了
  7. .enable(isOpen)
  8. // 分组名称
  9. .groupName("Demo")
  10. // 文档信息配置
  11. .apiInfo(apiInfo())
  12. // 配置扫描的接口
  13. .select()
  14. // 配置扫描哪里的接口
  15. .apis(RequestHandlerSelectors.basePackage("com.wc.demo.controller"))
  16. // 过滤请求
  17. .paths(PathSelectors.any())
  18. .build();
  19. }

这样就只能在dev环境中显示接口文档了


二、使用中的一些问题

1.接口文档不显示请求参数,不显示响应参数----注解描述

2.显示请求参数和响应参数,但是参数说明不显示

3.仅显示部分参数

解决思路:检查描述性注解是否使用得当,检查字段命名是否符合驼峰命名。

一般控制层使用@Api()描述类,@ApiOperation()描述接口,参数校验实体类中使用ApiModel()描述实体类@ApiModelProperty()描述属性@ApiParam()描述参数,且可对属性进行是否必传的描述注解@NotBlank()(一般用于String类型)@NotNull()等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/939922
推荐阅读
相关标签
  

闽ICP备14008679号