org.springframework.http.MediaType is Spring's type-safe representation of an HTTP media type (MIME type) such as application/json or text/html;charset=UTF-8. Spring uses it to read the Content-Type and Accept headers, decide which controller method and message converter handle a request, and set the Content-Type of the response.
It extends MimeType, adds HTTP-specific features such as quality values (q=0.8), and provides constants and parsing helpers so you do not compare raw strings.
A media type has a type, a subtype and optional parameters: application/json;charset=UTF-8 has type application, subtype json and a charset parameter. A wildcard such as */* or image/* is allowed in Accept headers. MediaType exposes these parts and common comparison operations.
Each constant also has a _VALUE string twin, such as MediaType.APPLICATION_JSON_VALUE. Use the string form in annotations, because annotation attributes must be compile-time constants.
The consumes and produces attributes of @RequestMapping and its shortcuts narrow a mapping by media type. Spring matches consumes against the request Content-Type and produces against the request Accept header.
Two methods may share a path as long as their produces values differ. A client that sends Accept: application/xml reaches the second method. If no method matches, Spring returns 406 Not Acceptable for a failing produces and 415 Unsupported Media Type for a failing consumes. XML output requires an XML converter on the classpath, such as jackson-dataformat-xml.
ResponseEntity lets you set the Content-Type per response, which is useful for downloads and for returning different types from one method.
For error bodies, use MediaType.APPLICATION_PROBLEM_JSON, which matches the RFC 9457 application/problem+json format that Spring's ProblemDetail produces.
Spring's ContentNegotiationManager determines the list of acceptable media types for a request. By default it reads the Accept header. Spring then:
produces or from the registered message converters.Accept types by specificity and quality value with MediaType.sortBySpecificityAndQuality.Content-Type.Path-extension negotiation (/report.json) and the format parameter are no longer enabled by default in modern Spring, and path-extension support has been removed from recent versions. If you want a query parameter, configure it explicitly in WebMvcConfigurer.configureContentNegotiation, and prefer the Accept header for APIs.
MediaType.APPLICATION_JSON_UTF8 and its _VALUE constant are deprecated since Spring Framework 5.2. JSON is always UTF-8 by specification (RFC 8259), so use APPLICATION_JSON. Spring no longer adds a charset parameter to JSON responses by default.
For custom vendor types, such as versioned APIs, define your own constants:
Then map produces = ApiMediaTypes.V2_VALUE. Spring also has dedicated API versioning support in newer releases, but media-type versioning with produces works in all supported versions.
MimeType is the generic base class. MediaType extends it with HTTP specifics such as quality values and more constants.
consumes matches the request Content-Type, so it restricts what the endpoint accepts. produces matches the request Accept header and sets the response Content-Type.
No available converter or handler can produce any of the types in the client's Accept header. Check produces and confirm the needed converter is on the classpath.
No. It is deprecated, and APPLICATION_JSON is correct because JSON is UTF-8 by definition.
MediaType gives Spring a typed model of Content-Type and Accept values. Use its constants in consumes, produces and ResponseEntity, rely on the Accept header for content negotiation, and avoid the deprecated APPLICATION_JSON_UTF8.