【Swagger】SpringBoot快速集成Swagger

目录:

  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

posted @ 2022-12-26 12:05  细胞何  阅读(60)  评论(0编辑  收藏  举报