使用 SimpleGraphQLServlet.Builder 时出现 NoClassDefFoundError
NoClassDefFoundError when using SimpleGraphQLServlet.Builder
我基于classSimpleGraphQLServlet
写了一个简单的GraphQL Servlet。此 class 的构造函数已弃用,建议改用构建器。所以我所做的是提供我自己的 servlet,它将请求转发到 SimpleGraphQLServlet
的实例,该实例可以在我的 servlet 的 init
方法中构建:
@WebServlet("/graphql")
public class GraphQLEndpoint extends HttpServlet {
SimpleGraphQLServlet graphQLServlet;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
graphQLServlet.service(req, resp);
}
@Override
public void init() throws ServletException {
graphQLServlet = SimpleGraphQLServlet.builder(buildSchema()).withInstrumentation(new TracingInstrumentation()).build();
}
private GraphQLSchema buildSchema() { /* ... */ }
}
但是,当创建SimpleGraphQLServlet
实例时,会抛出以下异常:
java.lang.NoClassDefFoundError: graphql/execution/instrumentation/NoOpInstrumentation
at graphql.servlet.SimpleGraphQLServlet$Builder.<init>(SimpleGraphQLServlet.java:134) ~[graphql-java-servlet-4.7.0.jar:na]
这很奇怪,因为 class 似乎在 jar 文件 graphql-java-8.0.jar
中可用。我声明了以下依赖项:
dependencies {
compile "com.graphql-java:graphql-java:8.0"
compile "com.graphql-java:graphql-java-tools:4.3.0"
compile "com.graphql-java:graphql-java-servlet:4.7.0"
compile "org.slf4j:slf4j-simple:1.7.25"
testCompile "junit:junit:3.8.1"
providedCompile "javax.servlet:javax.servlet-api:3.0.1"
}
我错过了什么?
我确实收到了@kaqqao 的提示,他告诉我,我的异常是 graphql-java 版本不匹配。我使用的 8.0 版本还不兼容。
现在我使用以下 graphql-java 依赖项,它工作正常:
compile "com.graphql-java:graphql-java:7.0"
compile "com.graphql-java:graphql-java-tools:4.3.0"
compile "com.graphql-java:graphql-java-servlet:4.7.0"
@kaqqao 以评论 "Overriding dependency versions is scary business." 结束。我同意 ;-)
2020 年答案:
implementation 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.10.0'
implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.10.0'
implementation 'com.graphql-java-kickstart:graphql-java-tools:5.6.1'
缺少那些依赖项
我基于classSimpleGraphQLServlet
写了一个简单的GraphQL Servlet。此 class 的构造函数已弃用,建议改用构建器。所以我所做的是提供我自己的 servlet,它将请求转发到 SimpleGraphQLServlet
的实例,该实例可以在我的 servlet 的 init
方法中构建:
@WebServlet("/graphql")
public class GraphQLEndpoint extends HttpServlet {
SimpleGraphQLServlet graphQLServlet;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
graphQLServlet.service(req, resp);
}
@Override
public void init() throws ServletException {
graphQLServlet = SimpleGraphQLServlet.builder(buildSchema()).withInstrumentation(new TracingInstrumentation()).build();
}
private GraphQLSchema buildSchema() { /* ... */ }
}
但是,当创建SimpleGraphQLServlet
实例时,会抛出以下异常:
java.lang.NoClassDefFoundError: graphql/execution/instrumentation/NoOpInstrumentation
at graphql.servlet.SimpleGraphQLServlet$Builder.<init>(SimpleGraphQLServlet.java:134) ~[graphql-java-servlet-4.7.0.jar:na]
这很奇怪,因为 class 似乎在 jar 文件 graphql-java-8.0.jar
中可用。我声明了以下依赖项:
dependencies {
compile "com.graphql-java:graphql-java:8.0"
compile "com.graphql-java:graphql-java-tools:4.3.0"
compile "com.graphql-java:graphql-java-servlet:4.7.0"
compile "org.slf4j:slf4j-simple:1.7.25"
testCompile "junit:junit:3.8.1"
providedCompile "javax.servlet:javax.servlet-api:3.0.1"
}
我错过了什么?
我确实收到了@kaqqao 的提示,他告诉我,我的异常是 graphql-java 版本不匹配。我使用的 8.0 版本还不兼容。
现在我使用以下 graphql-java 依赖项,它工作正常:
compile "com.graphql-java:graphql-java:7.0"
compile "com.graphql-java:graphql-java-tools:4.3.0"
compile "com.graphql-java:graphql-java-servlet:4.7.0"
@kaqqao 以评论 "Overriding dependency versions is scary business." 结束。我同意 ;-)
2020 年答案:
implementation 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.10.0'
implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.10.0'
implementation 'com.graphql-java-kickstart:graphql-java-tools:5.6.1'
缺少那些依赖项