配置swagger及版本问题

加入swagger依赖

<!--swagger-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索最新版本号-->
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger2</artifactId>-->
<!-- <version>2.6.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>2.6.1</version>-->
<!-- </dependency>-->

swagger配置类

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.X版本")
                .select()
                //这里指定Controller扫描包路径(项目路径也行)
                .apis(RequestHandlerSelectors.basePackage("com.xiaokun.device_status.sys.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("精准加药系统API接口文档")
                .description("CF系统API接口文档")
                .termsOfServiceUrl("http://localhost:8080/doc.html")
                .contact("古家杰")
                .version("1.0")
                .build();
    }
}

 

 

 

如果出现:Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

原因:swagger和springboot版本的问题,在springboot2.6.0中将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错,解决办法是切换回原先的AntPathMatcher

解决办法:application.yml中添加配置(解决异常问题):

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 

 
 
posted @ 2022-05-25 16:51  古家杰  阅读(613)  评论(0)    收藏  举报