首页 > 其他分享 >【Swagger】SpringBoot快速集成Swagger

【Swagger】SpringBoot快速集成Swagger

时间:2022-12-26 12:12:06浏览次数:67  
标签:集成 blog Swagger SpringBoot documentation swagger import springfox

目录:

  1、依赖
  2、配置类
  3、注解引用
  4、可能遇到的问题
  5、拓展

 

 

1、依赖

<!--swagger-->
<!-- 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、配置类(注意:照着改改就行,没什么操作性)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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
// 开启Swagger功能,也可加在启动类上
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webAllApi")
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.myself.swagger.controller"))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("swagger题目")
                .description("swagger描述")
                .version("1.0")
                .contact(new Contact("name","个人信息网站主页","邮箱地址"))
                .build();
    }
}

3、注解引用

(1)Api【用在Controller类上】
(2)ApiOperation【用在Api方法上】
(3)ApiParam【用在请求参数上】
(4)ApiModel【一般用在实体类上】
(5)ApiModelProperty【一般用在实体类的属性上】
(6)ApiIgnore【用于方法或类或参数上,表示这个方法或类被忽略】
(7)ApiImplicitParam【用在方法上,表示单独的请求参数,总体功能和@ApiParam类似】

4、可能遇到的问题

问题1:Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

  原因:这是因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.0 以上使用的是PathPatternMatcher

  解决:在application.properties里配置:spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

5、拓展

本文为spring boot快速集成swagger,如想更加详细了解每个步骤及对应参数意义可参考:https://blog.csdn.net/m0_55400356/article/details/126215411

 

 

小编不易,如果文章对您有帮助,可否来个三连,赏个赞也是好的呀!!!

 

博客参考:

SpringBoot继承Swagger:https://blog.csdn.net/zhouhengzhe/article/details/118063779

Swagget技术·SpringBoot继承Swagger框架详解!:https://blog.csdn.net/m0_55400356/article/details/126215411

documentationPluginsBootstrapper:https://blog.csdn.net/u010881625/article/details/125972017

springboot集成swagger报错踩坑documentationPluginsBootstrapper:https://blog.csdn.net/qq_44471484/article/details/123206680

标签:集成,blog,Swagger,SpringBoot,documentation,swagger,import,springfox
From: https://www.cnblogs.com/hwh000/p/17005455.html

相关文章

  • springboot利用Condition机制解决Kafka监听不到服务时报错的问题
    一般情况下,我们在写springboot使用Kafka监听的代码时,都是直接写个类,然后在方法上加个@KafkaListener就可以了,简单省事。就像下面这样@Component@Slf4jpublicclassKa......
  • SpringBoot2.x系列教程汇总-从入门到精通
    因为N没有分类归纳博客的功能,所以特写本帖汇总SpringBoot2.x系列教程,方便大家查阅!本套案例源码地址:https://gitee.com/sunyiyi/SpringBoot-demos​​SpringBoot2.x系列教程......
  • springboot 连接不上 redis 的三种解决方案!
    针对于这种情况,首先,我们最简单直接的方法就是需要确认Redis是否已经正常启动(验证方法:如果安装在Linux下的话可以使用ps-ef|grepredis来进行确认是否开启) 如果未开启,我......
  • 【AGC】集成付费下载服务sdk登录闪退
    问题背景:cp反馈集成付费下载sdk在正常登录时,出现登录闪退的报错,出现故障的手机型号操作系统版本是Android9,集成的付费下载的版本号是drm_v2.5.2.300。部分报错信息:​解决方......
  • 笑死,面试官又问我SpringBoot自动配置原理
    面试官:好久没见,甚是想念。今天来聊聊SpringBoot的自动配置吧?候选者:嗯,SpringBoot的自动配置我觉得是SpringBoot很重要的“特性”了。众所周知,SpringBoot有着“约定大于配置......
  • 【AGC】集成付费下载服务sdk登录闪退
    问题背景:cp反馈集成付费下载sdk在正常登录时,出现登录闪退的报错,出现故障的手机型号操作系统版本是Android9,集成的付费下载的版本号是drm_v2.5.2.300。部分报错信息:​ ......
  • SpringBoot的Maven项目使用SystemPath引用本地jar
    对于本地jar的maven引用,在不方便使用私有maven仓库的情况下,使用SystemPath方式引用还是比较合适的,这里以uid-generator-1.0.0-SNAPSHOT.jar这个本地包为例。1.将打好的包拷......
  • Spring Boot中使用Swagger2构建强大的RESTful API文档
    由于SpringBoot能够快速开发、便捷部署等特性,相信有很大一部分SpringBoot的用户会用来构建RESTfulAPI。而我们构建RESTfulAPI的目的通常都是由于多终端的原因,这些终端会......
  • springboot运行jar包报 "XXX中没有主清单属性"
    报错原因:打包后的jar文件中的MANIFEST.MF缺少项目启动项,即没有Main-Class解决:在项目pom.xml文件中添加插件spring-boot-maven-plugin:<build><plugins><plugin>......
  • SpringBoot加载相关注解
    springBoot加载@Configuration表明该类是一个配置类常常配合@Bean使用,让容器管理对象@Configuration(proxyBeanMethods=true)proxyBeanMethods=true表示@Configura......