Spring MVC uses HttpMessageConverterobjects 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.
Since Spring Boot 4.0, the recommended way is to register a ServerHttpMessageConvertersCustomizer bean. Boot calls it while it builds the converter list for the server side (Spring MVC).
What is ServerHttpMessageConvertersCustomizer
ServerHttpMessageConvertersCustomizer is an functional interface which used to customize a HttpMessageConverters (A new helper class starting Spring MVC 7, for building and configuring an immutable collection of HttpMessageConverter)
Definition of ServerHttpMessageConvertersCustomizerVersion: 4.2.0 package org.springframework.boot.http.converter.autoconfigure;
@FunctionalInterface
public interface ServerHttpMessageConvertersCustomizer {
void customize(ServerBuilder builder); 1
}
Version history
- Spring Boot 2.x and 3.x: You used Boot's
HttpMessageConverters class, or you registered converter beans directly. See the legacy tutorial here.
- Spring Boot 4.0: Boot's
HttpMessageConverters is deprecated, because it mixed client converters and server converters in one list. Now there are two separate customizers: ServerHttpMessageConvertersCustomizer for the server side and ClientHttpMessageConvertersCustomizer for the client side (for example RestClient).
- Spring Framework 7: It has its own
HttpMessageConverters class with a builder. Boot's customizers receive this builder.
Example
Adding an additional converter
Register a ServerHttpMessageConvertersCustomizer bean. Inside it, add your converter to the builder:
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.http.converter.autoconfigure.ServerHttpMessageConvertersCustomizer;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample1 {
@Bean
public ServerHttpMessageConvertersCustomizer additionalConverters() {
return builder ->
builder.addCustomConverter(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
Accessing endpoint in curl:
Output $ curl -s "http://localhost:8080" TheCustomConverter ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter ResourceRegionHttpMessageConverter AllEncompassingFormHttpMessageConverter JacksonJsonHttpMessageConverter
The converter is added to the default converters. It does not replace them.
Replacing the default converters
The replace default converters we can use ServerBuilder#disableDefaults
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.http.converter.autoconfigure.ServerHttpMessageConvertersCustomizer;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample2 {
@Bean
public ServerHttpMessageConvertersCustomizer additionalConverters() {
return builder ->
builder.disableDefaults()
.addCustomConverter(new TheCustomConverter());
}
@Bean
public MyController myController() {
return new MyController();
}
public static void main(String[] args) {
SpringApplication.run(MessageConvertersExample2.class,
args);
}
}
Run the above class and access the endpoint via curl:
Output $ curl -s "http://localhost:8080" TheCustomConverter AllEncompassingFormHttpMessageConverter
Notice that AllEncompassingFormHttpMessageConverter is still there. It handles HTML form data, and disableDefaults() does not remove it. All the other defaults are removed: the converters for String, byte[], Resource, XML and JSON.
Be careful with this option. Without those converters, your application cannot read or write JSON, XML or plain text unless your own converter supports it.
Replacing the default converters in Spring Boot 4.0.0 to 4.0.2
The method disableDefaults() was added in Spring Boot 4.0.3 (Spring Framework 7.0.4). The earlier 4.0.x versions do not have it, so the builder gives no way to turn off the default converters. In these versions, the only way is the older WebMvcConfigurer.configureMessageConverters(List) method. It gives you the full list of converters, so you can clear it and add your own. This method is deprecated in Spring Framework 7, which is why we use @SuppressWarnings("deprecation") here. Use it only if you cannot upgrade to 4.0.3 or later, and switch to disableDefaults() as soon as you can.
@SpringBootConfiguration
@EnableAutoConfiguration
public class MessageConvertersExample2 implements WebMvcConfigurer {
@Override
@SuppressWarnings("deprecation")
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.clear();
converters.add(new TheCustomConverter());
}
}
Also check out how to register the converters without Spring Boot here.
Example ProjectDependencies and Technologies Used: - Spring Boot 4.2.0
Corresponding Spring Framework Version 7.1.0 Version Compatibility: 4.0.0 - 4.2.0 Version compatibilities of spring-boot-starter-parent with this example: Java 17 min
- 4.0.0
- 4.0.1
- 4.0.2
- 4.0.3
- 4.0.4
- 4.0.5
- 4.0.6
- 4.0.7
- 4.0.8
- 4.0.9
- 4.0.10
- 4.1.0
- 4.1.1
- 4.1.2
- 4.1.3
- 4.2.0
Versions in green have been tested.
- spring-boot-starter-webmvc (Starter for using Spring MVC and Tomcat)
- JDK 25
|