Swagger of Java project reports an error: Java lang.NullPointerException: null

1, Foreword

Record the startup error when referring to Swagger configuration in SpringBoot project, which makes Swagger inaccessible, but the project can be started and access the background interface.

2, Error reporting information

1. Background error message:

2022-01-24 13:42:20.638 ERROR 18508 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Unable to scan documentation context default

java.lang.NullPointerException: null
	at springfox.documentation.schema.Example.equals(Example.java:131)
	at java.util.Objects.equals(Objects.java:59)
	at springfox.documentation.service.RequestParameter.equals(RequestParameter.java:132)
	at java.util.HashMap.putVal(HashMap.java:635)
	at java.util.HashMap.put(HashMap.java:612)
	at java.util.HashSet.add(HashSet.java:220)
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
	at springfox.documentation.spring.web.readers.operation.OperationParameterReader.apply(OperationParameterReader.java:93)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:144)
	at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:72)
	at springfox.documentation.spring.web.scanners.CachingOperationReader.lambda$new$0(CachingOperationReader.java:43)
	at java.util.HashMap.computeIfAbsent(HashMap.java:1127)
	at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:48)
	at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:72)
	at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:169)
	at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:67)
	at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.scanDocumentation(AbstractDocumentationPluginsBootstrapper.java:96)
	at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100)
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at com.bjnsc.ssis.DocumentApplication.main(DocumentApplication.java:29)

2. Swagger error message:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jan 24 12:00:18 CST 2022
There was an unexpected error (type=Not Found, status=404).

3, Problem analysis and Solutions

1, Since the equals method is rewritten in the source code, a null value detection error will be passed in at startup.

1. Click the first row of the error message to enter the source code

2. Note that the String type is not null judged in the source code.


3. There are two solutions:

  1. Modify the source code.
  2. Write this class again in the project (create a new springfox.documentation.schema directory under the com package, create an Example class under the schema package, copy and paste the source code, add @ override to the toString method, equals method and hashCode method, and add the judgment on the equals method to the blank judgment of String type).

2, Due to the public modifier used in the entity class.

1. Check the interfaces one by one. It is found that the attributes in the parameter object defined by an interface are decorated with public, and the corresponding getter and setter methods are generated with @ Data:

2. Debug the code and find it in springfox documentation. spring. web. readers. parameter. 168 lines in modelattributeparameterexpander class, with the following code:

3. According to the tracking code, when swagger generates the interface parameter record object, it will retain the attribute with getter method in the parameter object and the attribute decorated with public. If these two conditions are met at the same time, there will be a conflict. Change the public in the entity class to private.

3, Because multiple entity classes in the Controller have the same fields in the receive parameters.

1. Two receive parameter classes appear in the controller layer, and the same fields appear (in DocBaseForm and ApprovalRecordForm)

2. The reason is that after the integration of Swagger, an interface of the Controller layer receives two entity classes with the same name. As a result, the same fields in the two entity classes are integrated in one of them.

Keywords: Java Spring Boot swagger2

Added by jh21236 on Tue, 25 Jan 2022 11:45:45 +0200