Unable to scan documentation context default solution appears when Swagger2 starts
One, the problem
The local project has done the configuration of Swagger2, and the following error is reported during startup:
DocumentationPluginsBootstrapper : Unable to scan documentation context default
Controller file tested:
package com.quantsmart.controller;
import com.quantsmart.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: kaiyi
* @Date 2021/5/21 13:57
*/
@RestController
@Api(tags = "CoinCommon里面的测试接口")
public class TestController {
@GetMapping("/common/test")
@ApiOperation(value = "测试方法", authorizations = {@Authorization("Authorization")})
@ApiImplicitParams({
@ApiImplicitParam(name = "param", value = "参数1", dataType = "String", paramType = "query", example = "paramValue"),
@ApiImplicitParam(name = "param1", value = "参数2", dataType = "String", paramType = "query", example = "paramValue")
})
public R<String> testMethod(String param, String param2){
return R.ok("ok,哈哈哈,测试哇");
}
}
Swagger2 configuration
/config/swagger/SwaggerProperties.java
package com.quantsmart.config.swagger;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author: kaiyi
* @Date 2021/5/20 16:50
*/
@Data
@ConfigurationProperties(prefix = "swagger2")
public class SwaggerProperties {
/**
* 是否开启swagger
*/
private Boolean enabled;
/**
* 包扫描的路径
*/
private String basePackage;
/**
* 联系人的名称
*/
private String name ;
/**
* 联系人的主页
*/
private String url ;
/**
* 联系人的邮箱
*/
private String email ;
/**
* API的标题
*/
private String title ;
/**
* API的描述
*/
private String description ;
/**
* API的版本号
*/
private String version ;
/**
* API的服务团队
*/
private String termsOfServiceUrl ;
}
/config/swagger/SwaggerAutoConfiguration.java
package com.quantsmart.config.swagger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Arrays;
import java.util.List;
/**
* @author: kaiyi
* @Date 2021/5/20 16:50
*/
@Configuration
@EnableSwagger2 // 开启swagger2自动生成api文档的功能
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {
private SwaggerProperties swaggerProperties;
public SwaggerAutoConfiguration(SwaggerProperties swaggerProperties) {
this.swaggerProperties = swaggerProperties;
}
@Bean
public Docket docket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
// 安全的配置
docket.securitySchemes(securitySchemes()) // 安全规则
.securityContexts(securityContexts()); // 安全配置的上下问
return docket;
}
/**
* api 信息的简介
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().contact(
new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail())
)
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.version(swaggerProperties.getVersion())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.build();
}
/**
* 安全的规则配置
*
* @return
*/
private List<SecurityScheme> securitySchemes() {
return Arrays.asList(new ApiKey("Authorization", "Authorization", "Authorization"));
}
/**
* 安全的上下文
*
* @return
*/
private List<SecurityContext> securityContexts() {
return Arrays.asList(new SecurityContext(
Arrays.asList(new SecurityReference("Authorization", new AuthorizationScope[]{new AuthorizationScope("global", "accessResource")})),
PathSelectors.any()
));
}
}
Note that the apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
scan here is the controller package, and is the cause of the following error:
ERROR 4928 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Unable to scan documentation context default
java.lang.NullPointerException: null
at java.lang.String.startsWith(String.java:1405) ~[na:1.8.0_131]
at java.lang.String.startsWith(String.java:1434) ~[na:1.8.0_131]
at springfox.documentation.builders.RequestHandlerSelectors$4.apply(RequestHandlerSelectors.java:98) ~[springfox-core-2.9.2.jar:null]
at springfox.documentation.builders.RequestHandlerSelectors$4.apply(RequestHandlerSelectors.java:95) ~[springfox-core-2.9.2.jar:null]
at com.google.common.base.Present.transform(Present.java:75) ~[guava-20.0.jar:na]
at springfox.documentation.builders.RequestHandlerSelectors$5.apply(RequestHandlerSelectors.java:114) ~[springfox-core-2.9.2.jar:null]
at springfox.documentation.builders.RequestHandlerSelectors$5.apply(RequestHandlerSelectors.java:111) ~[springfox-core-2.9.2.jar:null]
at com.google.common.base.Predicates$AndPredicate.apply(Predicates.java:384) ~[guava-20.0.jar:na]
at com.google.common.base.Predicates$AndPredicate.apply(Predicates.java:384) ~[guava-20.0.jar:na]
at com.google.common.collect.Iterators$6.computeNext(Iterators.java:617) ~[guava-20.0.jar:na]
at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:145) ~[guava-20.0.jar:na]
at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:140) ~[guava-20.0.jar:na]
at springfox.documentation.spring.web.scanners.ApiListingReferenceScanner.scan(ApiListingReferenceScanner.java:48) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:67) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:101) [springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) [springfox-spring-web-2.9.2.jar:null]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:894) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at com.coin.TestCommonApplication.main(TestCommonApplication.java:12) [classes/:na]
Two, the solution
The problem has been found above, if I change apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
to the specific package I want to scan at this time
apis(RequestHandlerSelectors.basePackage(“com.quantsmart.controller”))
Restart at this time, you can find that the above error does not appear in the printed log, and a controller interface is printed and scanned in the DocumentationPluginsBootstrapper.
The above modification method is directly hard-coded, so we can configure swaggerProperties.getBasePackage() in the configuration file, the controller package path (that is, the basePackage attribute in the SwaggerProperties class), for example, add the controller package path application.yml
# Swagger2属性配置
swagger2:
base-package: com.quantsmart.controller # 需要指定扫描的包的路径
Then visit the http://localhost:8989/swagger-ui.html
page, you can see that you can successfully see the interface document page:
Corwien
CDH6 离线安装
Corwien赞 2阅读 1.9k
与RabbitMQ有关的一些知识
lpe234赞 8阅读 1.8k
万字详解,吃透 MongoDB!
JavaGuide赞 4阅读 377
Git操作不规范,战友提刀来相见!
王中阳Go赞 5阅读 2.1k评论 2
Redis 发布订阅模式:原理拆解并实现一个消息队列
码哥字节赞 6阅读 1.3k
PHP转Go实践:xjson解析神器「开源工具集」
王中阳Go赞 6阅读 1.4k评论 2
NB的Github项目,看到最后一个我惊呆了!
艾小仙赞 5阅读 1.6k评论 1
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。