gpt4 book ai didi

java - 使用spring RestTemplate发送post请求时出现异常

转载 作者:行者123 更新时间:2023-12-02 00:20:02 34 4
gpt4 key购买 nike

现在我想实现一个方法来使用 spring RestTemplate 而不是 httpClient 方法发送 post 请求。这是我最新的代码:

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代码吗?谢谢

最佳答案

您可以尝试使用下面的代码吗

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();
}

关于java - 使用spring RestTemplate发送post请求时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58078225/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com