swager/knife4j trivia record

Posted by eskick on Sun, 20 Feb 2022 15:15:09 +0100

1. Overview

Hello, I'm Ouyang Fangchao.
Today's event is still related to swagger (to be exact, knife4j). In the SpringBoot project integrating swagger, the dependencies introduced are as follows:

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>3.0.2</version>
		</dependency>

However, the project is started with the following error log:

2022-02-20 12:52:35.499 ERROR 18468 --- [           main] s.d.s.web.scanners.ApiDescriptionReader  : Skipping process path[/notice/export], method[export] as it has an error.

java.lang.NoSuchMethodError: com.fasterxml.classmate.members.ResolvedMethod.getAnnotations()Lcom/fasterxml/classmate/Annotations;
	at springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander.lambda$ignored$5(ModelAttributeParameterExpander.java:198)
	at java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
	at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
	at java.util.HashMap$KeySpliterator.tryAdvance(HashMap.java:1577)
	at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
	at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)

At first, I was deeply attracted by the following line of errors, because they are common in Java. I took it for granted that it was caused by the lack of jar package, but it was not.

java.lang.NoSuchMethodError: com.fasterxml.classmate.members.ResolvedMethod.getAnnotations()Lcom/fasterxml/classmate/Annotations;

2. Problem analysis

When you see the nosuchmethod, it's easy to think of the reason is the lack of dependency. Many places also say that you need the following dependency:

<dependency>
    <groupId>com.fasterxml</groupId>
    <artifactId>classmate</artifactId>
    <version>{version_num}</version>
</dependency>

But it didn't work after adding, and several groupids with com. ID were added in succession None of the fasterxml dependencies can solve this problem.
But I have to focus on the error log again. Alas, there seems to be an error, specifically:

ERROR 18468 --- [           main] s.d.s.web.scanners.ApiDescriptionReader  : Skipping process path[/notice/export], method[export] as it has an error

/The interface to which the notice/export path locates is an interface written by itself. The log says that it skipped the interface because there was an error in the export method. So it locates the source code and finds that the type of parameter it receives is a class, and there is no @ RequestBody annotation in front of it.

3. Problem solving

There are two ways to solve this problem.

3.1. Add @ RequestBody

Since the parameters of the error reporting method are of class type, add the @ RequestBody annotation in front of the class type, and then you can let swagger handle the method normally.

3.2. Ignore some parameters

Note that different versions may be different. According to the dependent version information I mentioned earlier, add the ignoredParameterTypes() method after build(), and its parameters are the class types of the parameters to be ignored

return new Docket(DocumentationType.SWAGGER_2)
                ...
                .build().ignoredParameterTypes(User.class);

4. Summary

There are some small problems encountered during the use of swagger/knife4j. Please record them.

Topics: Java JavaEE Spring Spring Boot swagger