如何解决Swagger 阅读器抛出 java.lang.NoSuchMethodError
我正在尝试在我的项目 (Sparkjava) 中创建 API 文档。 [我用这篇文章][1]我写了这个解析器:
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.jaxrs.Reader;
import io.swagger.models.*;
import spark.Route;
import java.util.Arrays;
import java.util.List;
public class SwaggerParser {
public static String getSwaggerjson(List<Route> routes) throws JsonProcessingException {
Swagger swagger = getSwagger(routes);
return swaggerToJson(swagger);
}
private static Swagger getSwagger(List<Route> routes) {
Swagger swagger = new Swagger();
swagger.info(new Info().description("User API")
.version("V1.0")
.title("Some random api for testing")
.contact(new Contact().name("Serol").url("https://serol.ro")));
swagger.schemes(Arrays.asList(Scheme.HTTP,Scheme.HTTPS));
swagger.consumes("application/json");
swagger.produces("application/json");
swagger.tag(new Tag().name("swagger"));
Reader reader = new Reader(swagger);
for (Route route : routes) {
try {
reader.read(route.getClass());
}catch (Throwable throwable){
System.out.println(throwable);
}
}
return reader.getSwagger();
}
public static String swaggerToJson(Swagger swagger) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
return objectMapper.writeValueAsstring(swagger);
}
}
它适用于我的测试应用程序,但不适用于实际项目。我为此编写了初始化测试:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.healthgorilla.portal.server.labnetwork.api.GetordersAPI;
import com.healthgorilla.portal.server.labnetwork.route.GetordersRoute;
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class SwaggerParserTest {
@Test
public void name() throws JsonProcessingException {
GetordersAPI api = mock(GetordersAPI.class);
when(api.exec()).thenReturn("");
String swaggerjson = SwaggerParser.getSwaggerjson(Collections.singletonList(new GetordersRoute(api)));
assertNotNull(swaggerjson);
}
}
它抛出一个异常:
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.introspect.AnnotatedMember.getType(Lcom/fasterxml/jackson/databind/type/TypeBindings;)Lcom/fasterxml/jackson/databind/JavaType;
但在测试项目中它有效 [1]:https://serol.ro/posts/2016/swagger_sparkjava/
编辑:
我发现了冲突。它在项目中更新了 jckson 版本。我改变了我的swagger依赖
<!-- swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.6.2</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.6.2</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
但这无济于事。有我的 Jackson 依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
解决方法
潜在的问题是您的依赖项(包括可传递的)与您的 swagger 版本不兼容。 我不知道您使用的是哪个构建系统,但在 maven 中可以使用
mvn dependency:tree
https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
在gradle中你可以尝试gradle -q dependencies --configuration scm
,更多在https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html
尝试检查您的依赖项及其版本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。