skip to content

Name the main built-in HttpMessageConverters and what Java type / media type each handles.

level: middleimportance: should knowfreq 55%

answer

  1. byte[] / String / Resource / Form / Jackson
  2. Jackson only if on classpath
  3. String converter default charset ISO-8859-1
  4. Form -> MultiValueMap, not DTO
  5. getSupportedMediaTypes drives negotiation

basics

~10 s

ByteArrayHttpMessageConverter (byte[]), StringHttpMessageConverter (String / text/plain), MappingJackson2HttpMessageConverter (JSON), and FormHttpMessageConverter (form-urlencoded / multipart into a MultiValueMap). Jackson is only registered if it's on the classpath.

solid answer

~30 s

The default set includes: ByteArrayHttpMessageConverter for byte[] with application/octet-stream and */*; StringHttpMessageConverter for String with text/plain and */* (default charset ISO-8859-1); ResourceHttpMessageConverter and ResourceRegionHttpMessageConverter for Resource / range requests; AllEncompassingFormHttpMessageConverter for application/x-www-form-urlencoded and multipart/form-data mapped to a MultiValueMap; and MappingJackson2HttpMessageConverter for application/json and application/*+json. XML support (Jackson XML or JAXB2) is added if those libraries are present. The Jackson JSON converter is registered only when Jackson is on the classpath — that's why Spring Boot's jackson-databind dependency 'just makes JSON work'. Each converter advertises its supported media types via getSupportedMediaTypes, which also drives content negotiation.

go deeper

for a junior

List the four big ones: byte[], String, form, Jackson/JSON.

for a middle

Map each converter to its Java type and media types, and know Jackson is conditional.

for a senior

Explain the ISO-8859-1 charset gotcha and that Form converter targets MultiValueMap, plus Resource range support.

for a principal

Discuss classpath-driven conditional registration and how getSupportedMediaTypes feeds content negotiation and producible-type computation.

## The default catalog Spring MVC's `WebMvcConfigurationSupport.addDefaultHttpMessageConverters(...)` registers, in order (simplified): | Converter | Java type | Media types | |---|---|---| | `ByteArrayHttpMessageConverter` | `byte[]` | `application/octet-stream`, `*/*` | | `StringHttpMessageConverter` | `String` | `text/plain`, `*/*` | | `ResourceHttpMessageConverter` | `Resource` | `*/*` (probes media type from the resource) | | `ResourceRegionHttpMessageConverter` | `ResourceRegion` | supports HTTP Range requests (partial content) | | `AllEncompassingFormHttpMessageConverter` | `MultiValueMap<String, ?>` | `application/x-www-form-urlencoded`, `multipart/form-data` | | `MappingJackson2HttpMessageConverter` | (almost any) object | `application/json`, `application/*+json` | Plus **conditionally**, based on classpath detection: - XML: `MappingJackson2XmlHttpMessageConverter` (jackson-dataformat-xml) or `Jaxb2RootElementHttpMessageConverter` (JAXB) → `application/xml`, `text/xml`. - `KotlinSerializationJsonHttpMessageConverter`, Gson/JSON-B variants, etc., when those libs are present. ## The key ones in detail **MappingJackson2HttpMessageConverter** — uses a Jackson `ObjectMapper` to bind JSON to POJOs. It `canWrite` almost any object type and `canRead` almost any target type, so it's the workhorse for REST APIs. Registered only if Jackson (`com.fasterxml.jackson`) is on the classpath. **StringHttpMessageConverter** — reads/writes `String`. Gotcha: its **default charset is ISO-8859-1** (not UTF-8) when the media type carries no charset, historically causing garbled non-ASCII output. It supports `*/*`, so a `String` return can win over Jackson for a `text/plain` or unspecified accept. **ByteArrayHttpMessageConverter** — `byte[]` in/out; useful for binary downloads. **FormHttpMessageConverter / AllEncompassingFormHttpMessageConverter** — reads `application/x-www-form-urlencoded` into a `MultiValueMap<String, String>` and writes it back; the 'AllEncompassing' subclass also handles `multipart/form-data` and can delegate part serialization to the other converters (so you can post JSON parts). Note it targets `MultiValueMap`, not arbitrary DTOs — form fields bound to a `@ModelAttribute` object go through data binding, not this converter. **ResourceHttpMessageConverter / ResourceRegionHttpMessageConverter** — stream `Resource`s (files) and support byte-range partial responses (e.g. video seeking). ## Why conditional registration matters Spring detects libraries with `ClassUtils.isPresent(...)`. Add `jackson-databind` → JSON converter appears; add `jackson-dataformat-xml` → XML converter appears. Spring Boot's starters simply put these on the classpath, which is why REST endpoints serialize JSON with zero config. Conversely, if Jackson is missing, an object return has no converter and Spring throws `HttpMediaTypeNotAcceptableException` (HTTP 406).

  • What target type does FormHttpMessageConverter read into, and how does that differ from binding a form to a DTO?
    It reads into a MultiValueMap<String, String> (or handles multipart). Binding form fields to a DTO via @ModelAttribute is a different mechanism — WebDataBinder / data binding — not message conversion.
  • Why does adding jackson-dataformat-xml change how an endpoint responds?
    Its presence causes Spring to conditionally register MappingJackson2XmlHttpMessageConverter, which supports application/xml. A client sending Accept: application/xml can now get XML from the same handler via content negotiation.

saying these in an interview costs you the question

  • Claiming FormHttpMessageConverter binds form data into an arbitrary @ModelAttribute DTO (that's data binding, not this converter).
  • Saying StringHttpMessageConverter defaults to UTF-8 (it historically defaults to ISO-8859-1).
  • Assuming JSON works without Jackson on the classpath.

context