You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RestTemplate调用PUT接口时读取HTTP消息失败求助

Fixing "failed to read HTTP message" Error with RestTemplate in Spring Boot

Hey Rajesh, let's break down this error you're facing when using RestTemplate to interact with your REST service. That "failed to read HTTP message" error usually points to issues with how your RestTemplate parses responses (or handles request bodies for update operations). Let's walk through the most likely fixes:

1. Fix Message Converter Configuration

Looking at your code, you've commented out the MappingJackson2HttpMessageConverter and StringHttpMessageConverter lines—this is probably the root cause. RestTemplate relies on these converters to handle JSON and string payloads, and without them, it can't parse the response from your endpoint.

First, make sure you have the Jackson dependency in your project:

Maven (pom.xml)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

Gradle (build.gradle)

implementation 'com.fasterxml.jackson.core:jackson-databind'

Then properly configure the converters in your code:

RestTemplate restTemplate = new RestTemplate();

// Initialize converters and add required ones
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());

// Attach converters to RestTemplate
restTemplate.setMessageConverters(messageConverters);

// Proceed with your request
HttpHeaders httpHeaders = restTemplate.headForHeaders("http://localhost:8080/getallblade");

2. Verify Endpoint Response Format

Double-check that the http://localhost:8080/getallblade endpoint returns valid JSON (if you plan to parse it into a Java object later). If the response uses a format your converters don't support (like XML when you only have JSON converters), you'll hit this error.

3. Correct Request Setup for Update Operations

Since you mentioned using RestTemplate to update a service, ensure your PUT/POST requests are configured properly:

  • Set the Content-Type header to application/json if sending a JSON payload
  • Make sure the request body matches the backend's expected structure

Example of a properly configured PUT request:

// Your update data transfer object
YourUpdateDto updateDto = new YourUpdateDto();
// Populate DTO with your data

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<YourUpdateDto> requestEntity = new HttpEntity<>(updateDto, headers);

// Send the update request
restTemplate.put("http://localhost:8080/your-update-endpoint", requestEntity);

4. Enable Debug Logging for Deep Diagnosis

If the above fixes don't work, enable debug logging to see exact request/response details. Add these lines to application.properties:

logging.level.org.springframework.web.client.RestTemplate=DEBUG
logging.level.org.springframework.http=DEBUG

This will log full request headers, body, and response content—helping you spot issues like malformed JSON or unexpected response types.


内容的提问来源于stack exchange,提问作者Rajesh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:32:16