我正在尝试在我的项目(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);
}
}
它在我的测试应用程序中工作,但在真实的项目中不工作。我为此编写了init test:
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>
2条答案
按热度按时间z2acfund1#
潜在的问题是,你的依赖项(包括可传递的)与你的swagger版本不兼容。我不知道您使用的是哪个构建系统,但在maven中可以使用
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中更多尝试检查您的依赖项及其版本。
nwlls2ji2#
检查pom.xml中的
springfox-boot-starter
依赖项。在从swagger2升级到swagger3的情况下,我忘记删除与swagger2的依赖关系,如springfox-boot-starter
。在那之后。