自己的spring cloud alibaba脚手架集成swagger3,运行的过程中报出null错误,项目启动没有什么问题接口可以访问,但是swagger功能不正常了。
2021-03-19 03:04:18.645 INFO 45081 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@467cd4b9, org.springframework.security.web.context.SecurityContextPersistenceFilter@2b06681c, org.springframework.security.web.header.HeaderWriterFilter@66d61298, org.springframework.security.web.authentication.logout.LogoutFilter@6fdb1a59, org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter@6e6f5842, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@67cd84f9, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@64b8eb96, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@69a3bf40, org.springframework.security.web.session.SessionManagementFilter@7dfec0bc, org.springframework.security.web.access.ExceptionTranslationFilter@49338f3, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@5df54296]
2021-03-19 03:04:19.477 ERROR 45081 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Unable to scan documentation context default
java.lang.NullPointerException: null
at springfox.documentation.schema.Example.equals(Example.java:94) ~[classes/:3.0.0]
at java.util.Objects.equals(Objects.java:59) ~[na:1.8.0_151]
at springfox.documentation.service.RequestParameter.equals(RequestParameter.java:132) ~[springfox-core-3.0.0.jar:3.0.0]
at java.util.HashMap.putVal(HashMap.java:635) ~[na:1.8.0_151]
at java.util.HashMap.put(HashMap.java:612) ~[na:1.8.0_151]
at java.util.HashSet.add(HashSet.java:220) ~[na:1.8.0_151]
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) ~[na:1.8.0_151]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_151]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_151]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_151]
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380) ~[na:1.8.0_151]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_151]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_151]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_151]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_151]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_151]
at springfox.documentation.spring.web.readers.operation.OperationParameterReader.apply(OperationParameterReader.java:93) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:144) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:72) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.scanners.CachingOperationReader.lambda$new$0(CachingOperationReader.java:43) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at java.util.HashMap.computeIfAbsent(HashMap.java:1127) ~[na:1.8.0_151]
at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:48) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:72) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:169) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:67) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.scanDocumentation(AbstractDocumentationPluginsBootstrapper.java:96) [springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) [springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) [springfox-spring-web-3.0.0.jar:3.0.0]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.1.RELEASE.j
经过断点调试,找到此类。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package springfox.documentation.schema;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import springfox.documentation.service.VendorExtension;
@JsonInclude(Include.NON_EMPTY)
public class Example {
private final String id;
private final String summary;
private final String description;
private final Object value;
private final String externalValue;
private final String mediaType;
private final List<VendorExtension> extensions = new ArrayList();
/** @deprecated */
@Deprecated
public Example(Object value) {
this.value = value;
this.mediaType = null;
this.externalValue = null;
this.id = null;
this.summary = null;
this.description = null;
}
/** @deprecated */
@Deprecated
public Example(String mediaType, Object value) {
this.mediaType = mediaType;
this.value = value;
this.externalValue = null;
this.id = null;
this.summary = null;
this.description = null;
}
public Example(String id, String summary, String description, Object value, String externalValue, String mediaType) {
this.id = id;
this.summary = summary;
this.description = description;
this.value = value;
this.externalValue = externalValue;
this.mediaType = mediaType;
}
public String getId() {
return this.id;
}
public String getSummary() {
return this.summary;
}
public String getDescription() {
return this.description;
}
public String getExternalValue() {
return this.externalValue;
}
public List<VendorExtension> getExtensions() {
return this.extensions;
}
public Object getValue() {
return this.value;
}
public Optional<String> getMediaType() {
return Optional.ofNullable(this.mediaType);
}
public String toString() {
return String.valueOf(this.value);
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
Example example = (Example)o;
return this.id.equals(example.id) && Objects.equals(this.summary, example.summary) && Objects.equals(this.description, example.description) && this.value.equals(example.value) && this.externalValue.equals(example.externalValue) && this.mediaType.equals(example.mediaType) && this.extensions.equals(example.extensions);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.id, this.summary, this.description, this.value, this.externalValue, this.mediaType, this.extensions});
}
}
类中的equals方法被重写了,框架启动过程中传入了字符串“”(具体怎么来的没有深究),因为没有判断字符串“空”导致return 语句执行报出null错误,swagger3因此被终止了。
解决方法 1 修改源码
解决方法2 在自己的项目中新建
springfox.documentation.schema包 创建 Example类 黏贴原Example类代码 在equals 方法里追加字符串空的判断,启动项目swagger3恢复正常了。