配置swagger2时
父pom文件
<properties> <swagger2.version>2.9.2</swagger2.version> <swagger-annotations.version>1.5.20</swagger-annotations.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>${swagger-annotations.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger2.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger2.version}</version> </dependency> </dependencies> </dependencyManagement>
子pom
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
2、swagger2配置
1、SwaggerProperties
package com.coin.config.swagger; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "swagger2") public class SwaggerProperties { /** * 包扫描的路径 */ 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 ; }
2、SwaggerAutoConfiguration
package com.coin.config.swagger; 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; @Configuration @EnableSwagger2 @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())) //扫描的controller包 .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() )); } }
注意这里的apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) //扫描的controller包
是产生下面错误的原因
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]
如果我此时将apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))改为我具体要扫描的包
apis(RequestHandlerSelectors.basePackage(“com.coin.controller”))
此时重启,可以发现打印的日志中没出现上面的错误了,且DocumentationPluginsBootstrapper 中打印扫描到一个controller接口了。
至于其他服务引用时会不会自己扫描这个后面再探讨