Close

Spring Web MVC - Spring MVC HttpMessageConverter Introduction

[Last Updated: Nov 25, 2026]

HttpMessageConverter is a strategy interface. It can convert an HTTP request body to a Java object, and it can convert a Java object to an HTTP response body.

The interface is in the package org.springframework.http.converter. It has one type parameter, which is the Java type that the converter can handle.

Definition of HttpMessageConverter

Version: 7.0.6
 package org.springframework.http.converter;
 public interface HttpMessageConverter<T> {
     boolean canRead(Class<?> clazz, 1
                     @Nullable MediaType mediaType);
     boolean canWrite(Class<?> clazz, 2
                      @Nullable MediaType mediaType);
     List<MediaType> getSupportedMediaTypes(); 3
     default List<MediaType> getSupportedMediaTypes(Class<?> clazz); 4
     T read(Class<? extends T> clazz, 5
            HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException;
     void write(T t, 6
                @Nullable MediaType contentType,
                HttpOutputMessage outputMessage)
                throws IOException, HttpMessageNotWritableException;
 }
1Indicates whether the given class can be read by this converter.
2Indicates whether the given class can be written by this converter.
3Return the list of media types supported by this converter.
4Return the list of media types supported by this converter for the given class. (Since 5.3.4)
5Read an object of the given type from the given input message, and returns it.
6Write a given object to the given output message.

Why do we need it?

HTTP messages are just bytes: text, JSON, XML, form data, images, and so on. Your Java code works with objects, like a String, a byte[] or your own Customer class. Something must translate between the two. In Spring MVC, this is the job of message converters.

  • Reading: a converter takes the bytes of the request body and creates a Java object from them.
  • Writing: a converter takes a Java object and writes it as bytes to the response body.

Because it is a strategy interface, there are many implementations. Each one knows one format or one Java type. Spring chooses the right one for each request. You do not call the converters yourself.

What does a converter know?

Every converter answers three simple questions:

  • Which Java types can I handle? For example, one converter handles String, and another handles byte[].
  • Which media types can I handle? A media type is a content format name, like text/plain or application/json.
  • How do I read and write? This is the actual conversion work.

In the interface, these questions are the methods canRead(), canWrite(), getSupportedMediaTypes(), read() and write().

Where is it used?

Spring MVC uses message converters in these places:

  • A controller method parameter marked with @RequestBody. The request body is converted to the parameter type.
  • A controller method marked with @ResponseBody, or any method of a @RestController. The returned object is converted to the response body.
  • HttpEntity and ResponseEntity, which give you the body together with the headers and the status code.
  • The HTTP clients RestTemplate and RestClient. They use converters to write request bodies and read response bodies.

Note: Spring WebFlux (the reactive web framework) does not use HttpMessageConverter. It has its own, similar types called HttpMessageReader and HttpMessageWriter.

Spring Core Converter vs MVC HttpMessageConverter

A Spring Converter is a general Spring Core interface that converts one Java type to another (for example, a String to an Integer) in one direction and with no knowledge of HTTP. It is used for annotations like @RequestParam and @PathVariable.

HttpMessageConverter, however, converts Java objects to an HTTP response body (e.g., in JSON format) and vice versa. It works in both directions based on media types such as application/json, and it is used for @RequestBody and @ResponseBody.

How does Spring choose a HttpMessageConverter?

Spring uses two HTTP headers to decide:

  • Reading a request: Spring looks at the Content-Type header of the request, which says what format the body has. It also looks at the Java type of the parameter. It asks each converter, in order, "can you read this?". The first converter that says yes does the work.
  • Writing a response: Spring looks at the Accept header of the request, which says what formats the client wants. It also looks at the Java type of the returned object. It asks each converter, in order, "can you write this?". The first converter that says yes does the work.

The order of the converters matters. This is why a converter you add can come before or after the default ones. If no converter matches, the request fails with an error: 415 Unsupported Media Type when a request body cannot be read, or 406 Not Acceptable when no response format fits the client.

Default converters

Spring MVC registers a set of built-in converters. Which ones are active depends on the libraries on your classpath. Here are some of the common ones:

  • ByteArrayHttpMessageConverter: for byte[]
  • StringHttpMessageConverter: for String
  • ResourceHttpMessageConverter: for Resource (files and similar)
  • SourceHttpMessageConverter: for XML Source types
  • AllEncompassingFormHttpMessageConverter: for HTML form data
  • A JSON converter, used when Jackson is on the classpath
  • An XML converter, used when an XML library (such as JAXB or Jackson XML) is on the classpath

Other converters exist for formats like Gson, Protobuf and Kotlin serialization. You can add them yourself.

Version history

  • Spring Framework 3.0: HttpMessageConverter was introduced as the central way to convert request and response bodies.
  • Spring Framework 6.x and earlier: The default JSON converter is MappingJackson2HttpMessageConverter, which uses Jackson 2.
  • Spring Framework 7 (2025): Jackson 3 support was added, with a new converter named JacksonJsonHttpMessageConverter. It is now the default JSON converter, and the old Jackson 2 converters are deprecated. Spring Framework 7 also added a builder class, also named HttpMessageConverters, to configure the converters of the client side and the server side separately.
  • Spring Boot 2.x and 3.x: Boot has its own helper class, HttpMessageConverters, to add or replace converters. See the legacy tutorial here.
  • Spring Boot 4.0 (2025): Boot's HttpMessageConverters class is deprecated. Use ServerHttpMessageConvertersCustomizer and ClientHttpMessageConvertersCustomizer instead. See the tutorial here.

In next tutorials we will see how HttpMessageConverter are used in Spring MVC.

See Also

Join