This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.2.0!spring-doc.cn

HTTP Message Conversion

The spring-web module contains the HttpMessageConverter interface for reading and writing the body of HTTP requests and responses through InputStream and OutputStream. HttpMessageConverter instances are used on the client side (for example, in the RestClient) and on the server side (for example, in Spring MVC REST controllers).spring-doc.cn

Concrete implementations for the main media (MIME) types are provided in the framework and are, by default, registered with the RestClient and RestTemplate on the client side and with RequestMappingHandlerAdapter on the server side (see Configuring Message Converters).spring-doc.cn

Several implementations of HttpMessageConverter are described below. Refer to the HttpMessageConverter Javadoc for the complete list. For all converters, a default media type is used, but you can override it by setting the supportedMediaTypes property.spring-doc.cn

Table 1. HttpMessageConverter Implementations
MessageConverter Description

StringHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write String instances from the HTTP request and response. By default, this converter supports all text media types(text/*) and writes with a Content-Type of text/plain.spring-doc.cn

FormHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write form data from the HTTP request and response. By default, this converter reads and writes the application/x-www-form-urlencoded media type. Form data is read from and written into a MultiValueMap<String, String>. The converter can also write (but not read) multipart data read from a MultiValueMap<String, Object>. By default, multipart/form-data is supported. Additional multipart subtypes can be supported for writing form data. Consult the javadoc for FormHttpMessageConverter for further details.spring-doc.cn

ByteArrayHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write byte arrays from the HTTP request and response. By default, this converter supports all media types (*/*) and writes with a Content-Type of application/octet-stream. You can override this by setting the supportedMediaTypes property and overriding getContentType(byte[]).spring-doc.cn

MarshallingHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write XML by using Spring’s Marshaller and Unmarshaller abstractions from the org.springframework.oxm package. This converter requires a Marshaller and Unmarshaller before it can be used. You can inject these through constructor or bean properties. By default, this converter supports text/xml and application/xml.spring-doc.cn

MappingJackson2HttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write JSON by using Jackson’s ObjectMapper. You can customize JSON mapping as needed through the use of Jackson’s provided annotations. When you need further control (for cases where custom JSON serializers/deserializers need to be provided for specific types), you can inject a custom ObjectMapper through the ObjectMapper property. By default, this converter supports application/json. This requires the com.fasterxml.jackson.core:jackson-databind dependency.spring-doc.cn

MappingJackson2XmlHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write XML by using Jackson XML extension’s XmlMapper. You can customize XML mapping as needed through the use of JAXB or Jackson’s provided annotations. When you need further control (for cases where custom XML serializers/deserializers need to be provided for specific types), you can inject a custom XmlMapper through the ObjectMapper property. By default, this converter supports application/xml. This requires the com.fasterxml.jackson.dataformat:jackson-dataformat-xml dependency.spring-doc.cn

MappingJackson2CborHttpMessageConverterspring-doc.cn

com.fasterxml.jackson.dataformat:jackson-dataformat-cborspring-doc.cn

SourceHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write javax.xml.transform.Source from the HTTP request and response. Only DOMSource, SAXSource, and StreamSource are supported. By default, this converter supports text/xml and application/xml.spring-doc.cn

GsonHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write JSON by using "Google Gson". This requires the com.google.code.gson:gson dependency.spring-doc.cn

JsonbHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write JSON by using the Jakarta Json Bind API. This requires the jakarta.json.bind:jakarta.json.bind-api dependency and an implementation available.spring-doc.cn

ProtobufHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write Protobuf messages in binary format with the "application/x-protobuf" content type. This requires the com.google.protobuf:protobuf-java dependency.spring-doc.cn

ProtobufJsonFormatHttpMessageConverterspring-doc.cn

An HttpMessageConverter implementation that can read and write JSON documents to and from Protobuf messages. This requires the com.google.protobuf:protobuf-java-util dependency.spring-doc.cn