我有下面的代码来提交一个post http调用
HttpHeaders headers = new HttpHeaders();
headers.set(TENANT_ID_HEADER, event.getTenantId());
HttpEntity<InputObj> requestEntity = new HttpEntity<>(inputObj, headers);
ResponseEntity<Object> responseEntity = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
Object.class
);
字符串
调用成功到达服务器,并返回200成功代码
ResponseEntity.ok().body("Successfully handled the request");
型
在调用完成后,我一直收到以下错误
Exception while performing api call with message : Error while extracting response for type [class java.lang.Object] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'Successfully': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Successfully': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
型
我尝试使用我自己的RestTemplate与转换器,而不是默认的org.springframework.web.client;
,但我仍然得到相同的错误
1条答案
按热度按时间p8h8hvxi1#
我假设你返回的响应头为“ContentType”application/json,这使得HttpMessageConverterExtractor选择一个json HttpMessageConverter来读取响应,而你的响应体不是有效的json,因此出现了异常。你应该将响应头“ContentType”改为“text/plain”。为了更好的可读性和可维护性,考虑使用ResponseEntity来接受响应。