现在,我想实现一种使用spring RestTemplate而不是httpClient方法来发送发布请求的方法。这是我的最新代码:
String url = "http://" + qeHost + qeEndpoint;
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cache-Control", "no-cache");
requestHeaders.add("Content-type", "text/plain");
requestHeaders.add("access-token", accessToken);
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("sql", request.getSql());
log.info("Sql Query sent to Query engine={}", request.getSql());
HttpEntity<MultiValueMap> requestEntity =
new HttpEntity(requestBody, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<QEResponseEntity> qeResponse =
restTemplate.postForEntity(url, requestEntity, QEResponseEntity.class);
return qeResponse.getBody();
}
它将在以下引发异常:
[nio-9091-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: No HttpMessageConverter for [org.springframework.util.LinkedMultiValueMap] and content type [text/plain]] with root cause.
org.springframework.web.client.RestClientException: No HttpMessageConverter for [org.springframework.util.LinkedMultiValueMap] and content type [text/plain]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:956) ~[spring-web-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732) ~[spring-web-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:444) ~[spring-web-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
at com.shopee.data.biz.brandseller.service.query.QueryServiceImpl.fetch(QueryServiceImpl.java:66) ~[brand-seller-biz-0.0.1-SNAPSHOT.jar!/:na]
at com.shopee.data.biz.brandseller.service.metricsmeta.MetricsMetaServiceImpl.sendQuery(MetricsMetaServiceImpl.java:242) ~[brand-seller-biz-0.0.1-SNAPSHOT.jar!/:na]
at com.shopee.data.biz.brandseller.service.metricsmeta.MetricsMetaServiceImpl.getBrandListFromItemProfile(MetricsMetaServiceImpl.java:149) ~[brand-seller-biz-0.0.1-SNAPSHOT.jar!/:na]
at com.shopee.data.biz.brandseller.service.metricsmeta.MetricsMetaServiceImpl.populateMetaData(MetricsMetaServiceImpl.java:94) ~[brand-seller-biz-0.0.1-SNAPSHOT.jar!/:na]
at com.shopee.data.webapi.brandseller.controller.metricsmeta.ScheduleMetaUpdateController.populateMetadataToMysql(ScheduleMetaUpdateController.java:32) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHan
下面是我的原始代码,它可以很好地工作:
log.info("Before getting http client");
CloseableHttpClient httpClient = HttpClientBuilder.create()/* .setDefaultRequestConfig(config) */.build();
HttpPost httpPost = new HttpPost("http://" + qeHost + qeEndpoint);
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Content-type", "text/plain");
httpPost.setHeader("access-token", accessToken);
log.info("Sql Query sent to Query engine={}", request.getSql());
StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(request), "UTF-8");
entity.setContentEncoding("UTF-8");
httpPost.setEntity(entity);
long startTime = System.currentTimeMillis();
CloseableHttpResponse response = httpClient.execute(httpPost);
long endTime = System.currentTimeMillis();
long timeTaken = endTime - startTime;
log.info("Time taken by Query Engine={}", timeTaken);
QEResponseEntity qeResponse = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(response.getEntity().getContent(), QEResponseEntity.class);
httpClient.close();
return qeResponse;
谁知道异常的原因以及如何修改restTemplate代码?谢谢
答案 0 :(得分:0)
请尝试以下代码,
String url = "http://" + qeHost + qeEndpoint;
MultiValueMap<String, String> requestHeaders = new HttpHeaders<>();
requestHeaders.add("Cache-Control", "no-cache");
requestHeaders.add("Content-type", "text/plain");
requestHeaders.add("access-token", accessToken);
log.info("Sql Query sent to Query engine={}", request.getSql());
HttpEntity<Object> requestEntity =
new HttpEntity<Object>(request.getSql(), requestHeaders);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<QEResponseEntity> qeResponse=
restTemplate.exchange(url, HttpMethod.POST, request,
QEResponseEntity.class);
return qeResponse.getBody();
}