Spring MVC uses HttpMessageConverter objects to turn Java objects into HTTP response bodies (JSON, XML, plain text, and so on) and to turn request bodies back into Java objects. Spring Boot registers a good set of default converters for you. Sometimes you need to add your own converter or change the defaults.
In Spring Boot 2.x and 3.x, the helper class HttpMessageConverters does this job. You register it as a bean, and Boot merges your converters with the default ones.
Version history
Spring Boot 2.x and 3.x: HttpMessageConverters is the standard way to manage converters. This tutorial covers this style.
Spring Boot 4.0: HttpMessageConverters is deprecated , because it mixed client converters and server converters in one list. A bean of this type still works in Boot 4, but you should not use it in new code. Use ServerHttpMessageConvertersCustomizer instead. See the separate tutorial about it here .
Spring Boot 4.2: HttpMessageConverters has been removed.
Using HttpMessageConverters
These public constructors are available:
HttpMessageConverters(HttpMessageConverter<?>... additionalConverters)
HttpMessageConverters(Collection<HttpMessageConverter<?>> additionalConverters)
HttpMessageConverters(boolean addDefaultConverters,
Collection<HttpMessageConverter<?>> converters)
Example
Project setup
build.gradle
pom.xml
build.gradle.kts
build.gradle
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.16</version>
<relativePath></relativePath>
</parent>
<groupId>com.logicbig.example</groupId>
<artifactId>message-converters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>25</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
plugins {
java
id("org.springframework.boot") version "3.5.16"
id("io.spring.dependency-management") version "1.1.7"
}
println(">>> EXECUTING SCRIPT: build.gradle.kts (Kotlin DSL)")
group = "com.logicbig.example"
version = "1.0-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_25
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
}
plugins {
id 'java'
id 'org.springframework.boot' version '3.5.16'
id 'io.spring.dependency-management' version '1.1.7'
}
println ">>> EXECUTING SCRIPT: build.gradle (Groovy DSL)"
group = 'com.logicbig.example'
version = '1.0-SNAPSHOT'
java {
sourceCompatibility = JavaVersion.VERSION_25
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
Creating a custom converter
package com.logicbig.example;
import org.springframework.http.converter.StringHttpMessageConverter;
public class TheCustomConverter extends StringHttpMessageConverter {
//just an empty custom converter
}
A controller to just return registered converters list
@RestController
public class MyController {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@GetMapping("/")
public String getMessageConverters() {
return handlerAdapter.getMessageConverters()
.stream()
.map(HttpMessageConverter::getClass)
.map(Class::getSimpleName)
.collect(Collectors.joining("\n"));
}
}
Main class, adding additional converters
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample1 {
@Bean
public HttpMessageConverters additionalConverters() {
return new HttpMessageConverters(new TheCustomConverter());
}
@Bean
public MyController myController() {
return new MyController();
}
public static void main(String[] args) {
SpringApplication.run(MessageConvertersExample1.class,
args);
}
}
To run Spring Boot application you can run your main class from IDE, or execute it from the command line using your build tool:
Maven: mvn spring-boot:run
Gradle: gradle bootRun
After running the main class, access the endpoint in curl:
Output $ curl curl -s "http://localhost:8080" ByteArrayHttpMessageConverter TheCustomConverter StringHttpMessageConverter ResourceHttpMessageConverter ResourceRegionHttpMessageConverter SourceHttpMessageConverter AllEncompassingFormHttpMessageConverter MappingJackson2HttpMessageConverter Jaxb2RootElementHttpMessageConverter
Main class, replacing default converters
Pass false as the first argument. This tells Boot not to add the default converters:
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample2 {
@Bean
public HttpMessageConverters replaceConverters() {
return new HttpMessageConverters(
false, Arrays.asList(new TheCustomConverter()));
}
@Bean
public MyController myController() {
return new MyController();
}
public static void main(String[] args) {
SpringApplication.run(MessageConvertersExample2.class,
args);
}
}
Run above main class and access the endpoint in curl:
Output $ curl curl -s "http://localhost:8080" TheCustomConverter
Main class, registering the HttpMessageConverter as a bean
You can also register an HttpMessageConverter directly as a bean. Boot finds it and adds it to the default converters:
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample3 {
@Bean
public HttpMessageConverter<?> messageConverterAsBean() {
return new TheCustomConverter();
}
@Bean
public MyController myController() {
return new MyController();
}
public static void main(String[] args) {
SpringApplication.run(MessageConvertersExample3.class,
args);
}
}
It produces the same output as the first example (adding additional converters), because the default converters are kept.
Output $ curl curl -s "http://localhost:8080" ByteArrayHttpMessageConverter TheCustomConverter StringHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter ResourceRegionHttpMessageConverter SourceHttpMessageConverter AllEncompassingFormHttpMessageConverter MappingJackson2HttpMessageConverter MappingJackson2HttpMessageConverter Jaxb2RootElementHttpMessageConverter
Also check out how to register the converters without Spring Boot here .
Example Project Dependencies and Technologies Used:
Spring Boot 3.5.16 Corresponding Spring Framework Version 6.2.19 Version Compatibility: 2.0.0.RELEASE - 3.5.16
spring-boot-starter-web (Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container) JDK 25