![](/img/trans.png)
[英]GraphQL api consuming with spring boot Resttemplate resulting in {"errors":[{"message":"No query string was present"}]} always
[英]Consuming GraphQL API in Spring Boot controller
我需要在Spring Boot控制器中使用第三方提供的graphQL API,我按此处所述进行了尝试,但收到400错误。 下面是代码:
@SuppressWarnings("unchecked")
@RequestMapping(value = "/graphQLTest")
public ResponseEntity<?> testGraphQL() throws JsonProcessingException {
CloseableHttpClient client= null;
CloseableHttpResponse response= null;
client= HttpClients.createDefault();
HttpPost httpPost= new HttpPost("http://172.30.66.149:3000/graphql");
httpPost.addHeader("Accept","application/json");
try {
StringEntity entity= new StringEntity("{\"query\":\"{hello}\"}");
httpPost.setEntity(entity);
response= client.execute(httpPost);
System.out.println("response: "+response);
}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
return null;
}
当我尝试在邮递员中调用相同的URL时,得到响应,以下是屏幕截图:
但是,当我在控制器中尝试时,出现以下错误:谁能帮我解决我该如何使用某人的GraphQL API的问题。
Error: HttpResponseProxy{HTTP/1.1 400 Bad Request [X-Powered-By: Express, Content-Type: application/json; charset=utf-8, Content-Length: 53, ETag: W/"35-1vperDe+r7EpHry/i+J3wg", Date: Tue, 24 Apr 2018 12:32:52 GMT, Connection: keep-alive] ResponseEntityProxy{[Content-Type: application/json; charset=utf-8,Content-Length: 53,Chunked: false]}}
我只是尝试通过jersey api
调用我的graphQL API,如下所示,我能够获得响应。
@SuppressWarnings("unchecked")
@RequestMapping(value = "/graphQLTest")
public ResponseEntity<?> testGraphQL() throws JsonProcessingException {
JSONObject jsonObj = new JSONObject();
jsonObj.put("query", "{\n" +
" hello\n" +
"}");
Client client = Client.create();
String URL = "http://172.30.66.149:3000";
WebResource webResource = client.resource(URL);
ClientResponse response = webResource.type("application/json").accept("application/json").post(ClientResponse.class, jsonObj.toString());
String output = response.getEntity(String.class);
System.out.println(output);
return null;
}
有没有其他方法可以进行此调用,或者特别是graphQL方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.