2

我写了一个基于 class 的简单 GraphQL Servlet SimpleGraphQLServlet。此类的构造函数已弃用,建议改用生成器。所以我所做的是提供我自己的 servlet,它将请求转发到SimpleGraphQLServlet可以在我的 servletinit方法中构建的实例:

@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]

这很奇怪,因为该类似乎在 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"
}

我错过了什么?

4

2 回答 2

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 以评论“覆盖依赖版本是可怕的事情”结束。我同意 ;-)

于 2018-04-17T18:16:26.060 回答
1

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'

缺少那些依赖项

于 2020-01-21T19:30:18.270 回答