|
| 1 | +[[message-converters]] |
| 2 | += HTTP Message Conversion |
| 3 | + |
| 4 | +[.small]#xref:web/webflux/reactive-spring.adoc#webflux-codecs[See equivalent in the Reactive stack]# |
| 5 | + |
| 6 | +The `spring-web` module contains the `HttpMessageConverter` interface for reading and writing the body of HTTP requests and responses through `InputStream` and `OutputStream`. |
| 7 | +`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). |
| 8 | + |
| 9 | +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 xref:web/webmvc/mvc-config/message-converters.adoc[Configuring Message Converters]). |
| 10 | + |
| 11 | +Several implementations of `HttpMessageConverter` are described below. |
| 12 | +Refer to the {spring-framework-api}/http/converter/HttpMessageConverter.html[`HttpMessageConverter` Javadoc] for the complete list. |
| 13 | +For all converters, a default media type is used, but you can override it by setting the `supportedMediaTypes` property. |
| 14 | + |
| 15 | +[[rest-message-converters-tbl]] |
| 16 | +.HttpMessageConverter Implementations |
| 17 | +[cols="1,3"] |
| 18 | +|=== |
| 19 | +| MessageConverter | Description |
| 20 | + |
| 21 | +| `StringHttpMessageConverter` |
| 22 | +| An `HttpMessageConverter` implementation that can read and write `String` instances from the HTTP request and response. |
| 23 | +By default, this converter supports all text media types(`text/{asterisk}`) and writes with a `Content-Type` of `text/plain`. |
| 24 | + |
| 25 | +| `FormHttpMessageConverter` |
| 26 | +| An `HttpMessageConverter` implementation that can read and write form data from the HTTP request and response. |
| 27 | +By default, this converter reads and writes the `application/x-www-form-urlencoded` media type. |
| 28 | +Form data is read from and written into a `MultiValueMap<String, String>`. |
| 29 | +The converter can also write (but not read) multipart data read from a `MultiValueMap<String, Object>`. |
| 30 | +By default, `multipart/form-data` is supported. |
| 31 | +Additional multipart subtypes can be supported for writing form data. |
| 32 | +Consult the javadoc for `FormHttpMessageConverter` for further details. |
| 33 | + |
| 34 | +| `ByteArrayHttpMessageConverter` |
| 35 | +| An `HttpMessageConverter` implementation that can read and write byte arrays from the HTTP request and response. |
| 36 | +By default, this converter supports all media types (`{asterisk}/{asterisk}`) and writes with a `Content-Type` of `application/octet-stream`. |
| 37 | +You can override this by setting the `supportedMediaTypes` property and overriding `getContentType(byte[])`. |
| 38 | + |
| 39 | +| `MarshallingHttpMessageConverter` |
| 40 | +| An `HttpMessageConverter` implementation that can read and write XML by using Spring's `Marshaller` and `Unmarshaller` abstractions from the `org.springframework.oxm` package. |
| 41 | +This converter requires a `Marshaller` and `Unmarshaller` before it can be used. |
| 42 | +You can inject these through constructor or bean properties. |
| 43 | +By default, this converter supports `text/xml` and `application/xml`. |
| 44 | + |
| 45 | +| `MappingJackson2HttpMessageConverter` |
| 46 | +| An `HttpMessageConverter` implementation that can read and write JSON by using Jackson's `ObjectMapper`. |
| 47 | +You can customize JSON mapping as needed through the use of Jackson's provided annotations. |
| 48 | +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. |
| 49 | +By default, this converter supports `application/json`. This requires the `com.fasterxml.jackson.core:jackson-databind` dependency. |
| 50 | + |
| 51 | +| `MappingJackson2XmlHttpMessageConverter` |
| 52 | +| An `HttpMessageConverter` implementation that can read and write XML by using {jackson-github-org}/jackson-dataformat-xml[Jackson XML] extension's `XmlMapper`. |
| 53 | +You can customize XML mapping as needed through the use of JAXB or Jackson's provided annotations. |
| 54 | +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. |
| 55 | +By default, this converter supports `application/xml`. This requires the `com.fasterxml.jackson.dataformat:jackson-dataformat-xml` dependency. |
| 56 | + |
| 57 | +| `MappingJackson2CborHttpMessageConverter` |
| 58 | +| `com.fasterxml.jackson.dataformat:jackson-dataformat-cbor` |
| 59 | + |
| 60 | +| `SourceHttpMessageConverter` |
| 61 | +| An `HttpMessageConverter` implementation that can read and write `javax.xml.transform.Source` from the HTTP request and response. |
| 62 | +Only `DOMSource`, `SAXSource`, and `StreamSource` are supported. |
| 63 | +By default, this converter supports `text/xml` and `application/xml`. |
| 64 | + |
| 65 | +| `GsonHttpMessageConverter` |
| 66 | +| An `HttpMessageConverter` implementation that can read and write JSON by using "Google Gson". |
| 67 | +This requires the `com.google.code.gson:gson` dependency. |
| 68 | + |
| 69 | +| `JsonbHttpMessageConverter` |
| 70 | +| An `HttpMessageConverter` implementation that can read and write JSON by using the Jakarta Json Bind API. |
| 71 | +This requires the `jakarta.json.bind:jakarta.json.bind-api` dependency and an implementation available. |
| 72 | + |
| 73 | +| `ProtobufHttpMessageConverter` |
| 74 | +| An `HttpMessageConverter` implementation that can read and write Protobuf messages in binary format with the `"application/x-protobuf"` |
| 75 | +content type. This requires the `com.google.protobuf:protobuf-java` dependency. |
| 76 | + |
| 77 | +| `ProtobufJsonFormatHttpMessageConverter` |
| 78 | +| An `HttpMessageConverter` implementation that can read and write JSON documents to and from Protobuf messages. |
| 79 | +This requires the `com.google.protobuf:protobuf-java-util` dependency. |
| 80 | + |
| 81 | +|=== |
| 82 | + |
| 83 | + |
0 commit comments