我正在学习使用JAVA进行GraphQL客户端到服务器的通信。我现在正在使用Apollo-Client。通过Java客户端执行查询时遇到问题。
我的GraphQL模式:
type Query {
bookById(id: ID): Book
}
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
type Author {
id: ID
firstName: String
lastName: String
}
我的查询架构
query BookById($id: ID!) {
bookById(id: $id) {
name
}
}
我已经从Apollo Gradle插件生成了Java客户端代码。
下面是我的客户代码:
public class Client {
private static final String BASE_URL = "https://localhost:8080/graphql";
private ApolloClient apolloClient;
private AtomicInteger result;
@Test
public void connectServer() throws InterruptedException {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.build();
apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
BookByIdQuery bookByIdQuery = new BookByIdQuery("book-1");
ApolloQueryCall<BookByIdQuery.Data> query = apolloClient
.query(bookByIdQuery);
System.out.println(bookByIdQuery.queryDocument());
query.enqueue(new CallBackImpl());
Thread.currentThread().join();
}
private class CallBackImpl extends ApolloCall.Callback<BookByIdQuery.Data> {
@Override
public void onResponse(@Nonnull Response<BookByIdQuery.Data> response) {
log.info(response.data().bookById().name());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
log.info("Fail:" + e.getMessage());
}
}
}
当我运行此客户端程序时,它会抛出:
Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException
at Client$CallBackImpl.onFailure(Client.java:61)
at com.apollographql.apollo.ApolloCall$Callback.onNetworkError(ApolloCall.java:115)
at com.apollographql.apollo.internal.RealApolloCall$1.onFailure(RealApolloCall.java:238)
at com.apollographql.apollo.internal.interceptor.ApolloCacheInterceptor$1$1.onFailure(ApolloCacheInterceptor.java:91)
at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onFailure(ApolloParseInterceptor.java:63)
at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$1$1.onFailure(ApolloServerInterceptor.java:89)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:148)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
服务器端错误:
ava.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:414) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.14.jar:9.0.14]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.14.jar:9.0.14]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
从失眠症患者的同一呼叫在URL https://localhost:8080/graphql上正常运行:
答案 0 :(得分:0)
在BASE_URL中将https替换为http。 服务器无法理解https请求。