Fix RestTemplate Conversion Error in Spring
When working with REST APIs in Spring Boot, developers often use RestTemplate to send HTTP requests and process responses. One common issue developers face is the “No suitable HttpMessageConverter” exception. This error usually occurs when Spring cannot convert the request or response body into the required Java object format. In this article, we will understand why this error occurs, how Spring HttpMessageConverters work, and how to fix the issue with a complete Spring Boot example.
1. Overview
Spring Boot uses HttpMessageConverter classes internally to convert:
- Java Objects → JSON/XML (Request Body)
- JSON/XML → Java Objects (Response Body)
Whenever RestTemplate sends or receives data, Spring searches for a suitable converter. If no matching converter is available, Spring throws the following exception: No suitable HttpMessageConverter found for request type. This problem commonly appears when:
- Jackson dependency is missing
- Incorrect Content-Type is used
- Unsupported media type is returned
- Custom object mapping is incorrect
- XML/JSON converter is unavailable
1.1 Understanding the Problem
Suppose we are sending a Java object using RestTemplate:
Employee employee = new Employee(1, "John"); restTemplate.postForObject(url, employee, String.class);
Spring tries to convert the Employee object into JSON before sending the request. For this conversion, Spring requires the Jackson library and MappingJackson2HttpMessageConverter. If Jackson dependency is not available, Spring cannot serialize the object and throws: Could not write request: no suitable HttpMessageConverter found. Similarly, if the server returns XML but the application only supports JSON, the same issue can occur while reading the response.
1.2 Common Reasons Behind the Error
The No suitable HttpMessageConverter exception can occur due to multiple configuration or data conversion issues in a Spring Boot application. The following table highlights the most common causes along with their solutions.
| Problem | Solution |
|---|---|
| Jackson dependency missing | Add spring-boot-starter-web |
| Unsupported Content-Type | Use application/json |
| Missing getters/setters | Add proper POJO methods |
| Server returns XML | Add XML converter dependency |
| Invalid response mapping | Use correct response class |
2. Code Explanation
Add the required Spring Web dependency in pom.xml. Spring Boot automatically adds Jackson support.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.1 Create Employee Model Class
The Employee class is a simple Java POJO (Plain Old Java Object) used to store employee-related data. Spring uses this class to automatically convert Java objects into JSON and JSON back into Java objects during REST communication.
This model class contains employee properties along with constructors, getters, and setters required for serialization and deserialization.
package com.example.demo.model;
public class Employee {
private int id;
private String name;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In this code, the Employee class defines two fields: id and name. The default constructor is required by Spring and Jackson during object deserialization, while the parameterized constructor helps initialize objects easily. The getter and setter methods provide access to private variables and allow Spring’s HttpMessageConverter to serialize the object into JSON format and deserialize JSON responses back into Java objects automatically.
2.2 Create RestTemplate Bean
In Spring Boot, RestTemplate is commonly used to perform REST API operations such as GET, POST, PUT, and DELETE requests. To make RestTemplate available throughout the application, it is usually configured as a Spring Bean.
Creating a centralized RestTemplate bean helps manage REST communication efficiently and allows additional custom configurations such as message converters, interceptors, and timeout settings.
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
In this code, the @Configuration annotation marks the class as a Spring configuration class, while the @Bean annotation tells Spring to create and manage the RestTemplate object inside the application context. Once registered, the RestTemplate bean can be injected into controllers, services, or runner classes using dependency injection. Spring internally attaches suitable HttpMessageConverters to this RestTemplate instance so it can automatically convert Java objects into JSON/XML and process HTTP responses correctly.
2.3 Create REST Controller
The REST controller handles incoming HTTP requests and processes employee-related operations. In this example, the controller receives Employee data sent from the RestTemplate client in JSON format.
Spring automatically converts the incoming JSON request body into an Employee Java object using HttpMessageConverter and Jackson.
package com.example.demo.controller;
import com.example.demo.model.Employee;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping
public String addEmployee(@RequestBody Employee employee) {
return "Employee Added : " + employee.getName();
}
}
In this code, the @RestController annotation marks the class as a REST API controller, while @RequestMapping("/employees") defines the base URL for all endpoints inside the controller. The @PostMapping annotation handles HTTP POST requests, and the @RequestBody annotation tells Spring to convert the incoming JSON request into an Employee object automatically. Once the conversion is completed successfully, the method returns a confirmation message containing the employee name received from the client request.
2.4 Create RestTemplate Client
The RestTemplate client is responsible for sending HTTP requests to the REST API endpoint. In this example, the client sends Employee data to the server using an HTTP POST request.
Spring automatically converts the Employee Java object into JSON format before sending the request and converts the server response back into a String object.
package com.example.demo.runner;
import com.example.demo.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private RestTemplate restTemplate;
@Override
public void run(String... args) throws Exception {
String url = "http://localhost:8080/employees";
Employee employee = new Employee(101, "John");
String response = restTemplate.postForObject(
url,
employee,
String.class
);
System.out.println(response);
}
}
In this code, the @Component annotation registers the class as a Spring-managed bean, and the class implements CommandLineRunner so the code executes automatically when the Spring Boot application starts. The @Autowired annotation injects the RestTemplate bean created earlier. Inside the run() method, a REST endpoint URL is defined, and an Employee object is created with sample data. The postForObject() method sends the Employee object to the server as a JSON request body using an HTTP POST request. Spring internally uses HttpMessageConverter and Jackson to serialize the Employee object into JSON and deserialize the server response into a String object, which is then printed to the console.
2.5 Main Spring Boot Application
The main Spring Boot application class acts as the entry point of the application. It is responsible for starting the embedded server, loading the Spring application context, and initializing all configured beans and components.
When the application starts, Spring Boot automatically scans the project packages, configures dependencies, and executes the RestTemplate client created earlier.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
In this code, the @SpringBootApplication annotation is a combination of @Configuration, @EnableAutoConfiguration, and @ComponentScan. It enables Spring Boot’s auto-configuration mechanism and scans the application packages for controllers, beans, and configuration classes automatically. The main() method uses SpringApplication.run() to bootstrap and launch the application. Once the application starts successfully, Spring creates all required beans including RestTemplate, loads the REST controller, and executes the CommandLineRunner implementation that sends the Employee request to the REST API endpoint.
2.6 Code Run and Output
After creating all required classes and configurations, the Spring Boot application can be executed to test the REST API communication. When the application starts, the CommandLineRunner implementation automatically triggers the RestTemplate client and sends the Employee object to the REST controller endpoint. Spring internally uses MappingJackson2HttpMessageConverter to convert the Employee Java object into JSON format before sending the request. The controller then receives the JSON request, converts it back into an Employee object, and returns a success response.
Execute the application using your IDE or Maven command: mvn spring-boot:run
The following JSON request body is automatically generated from the Employee object:
{
"id": 101,
"name": "John"
}
After successful request processing, the following output appears in the console: Employee Added : John
2.6.1 Error Scenario Without Jackson Dependency
If the Jackson dependency or JSON converter is missing, the application throws the following exception during request processing:
Could not write request: no suitable HttpMessageConverter found for request type: com.example.demo.model.Employee
This happens because Spring cannot serialize the Employee object into JSON format without a compatible HttpMessageConverter implementation.
3. Conclusion
The “No suitable HttpMessageConverter” error in Spring RestTemplate occurs when Spring cannot convert request or response data into the required format. In most cases, the issue happens due to missing Jackson dependencies, unsupported media types, or incorrect object mapping. By adding the correct dependencies, configuring RestTemplate properly, and ensuring valid request/response formats, the issue can be resolved easily. Spring Boot automatically provides most converters, making REST communication simple and efficient.




