MediaType in Spring MVC — Content Type Constants
Fast recall
Quick revision
Core answer
Awareness of MediaType constants as best practice over hardcoded strings
Using hardcoded strings like application slash json instead of MediaType.APPLICATION_JSON_VALUE everywhere
Mention MediaType can be parsed, compared, and checked for compatibility using methods like isCompatibleWith
Interview-ready guide
Interview answer
MediaType is a Spring class that holds constants for MIME types, which are the format strings like application slash json or text slash html.
Instead of writing the string directly, I use MediaType dot APPLICATION_JSON_VALUE. The reason is it prevents typos. If I write 'application slash jason' by mistake, my endpoint returns 406 errors and it's hard to debug.
With the constant, I get a compile error immediately if I mistype it. There are two forms of each constant. The one with _VALUE at the end like APPLICATION_JSON_VALUE is a string, and that's what I use in annotations like GetMapping produces.
The one without _VALUE like APPLICATION_JSON is a MediaType object, and that has methods for comparing and checking compatibility.
For example, if I'm writing custom logic that needs to check if a request is JSON, I can use isCompatibleWith to handle wildcards. Common constants I use are APPLICATION_JSON_VALUE for REST APIs, MULTIPART_FORM_DATA_VALUE for file uploads, and APPLICATION_OCTET_STREAM_VALUE for binary file downloads. A mistake I've seen is people using the wrong form in annotations.
They write produces equals MediaType dot APPLICATION_JSON without the _VALUE, and it doesn't compile because annotations need string literals. Using MediaType constants is just a best practice. It makes code more readable and safer because you're not scattering string literals everywhere.
In a real project, every produces and consumes should use these constants.
MediaType in Spring MVC — Content Type Constants
Direct Answer
MediaType is a Spring class containing constants for MIME type strings used in produces, consumes, and Content-Type headers. Key constants include APPLICATION_JSON_VALUE for application/json, APPLICATION_XML_VALUE, TEXT_PLAIN_VALUE, and MULTIPART_FORM_DATA_VALUE. Using these constants prevents typos and makes code more maintainable.
Code Example
@RestController
@RequestMapping("/api/files")
public class FileController {
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public FileMetadata getFileInfo(@PathVariable String id) {
return new FileMetadata(id, "document.pdf", 2048000L);
}
@PostMapping(
value = "/upload",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<FileMetadata> uploadFile(
@RequestParam("file") MultipartFile file
) {
// consumes checks Content-Type: multipart/form-data
var metadata = fileService.save(file);
return ResponseEntity.ok(metadata);
}
@GetMapping(value = "/{id}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> downloadFile(@PathVariable String id) {
var fileData = fileService.getFileBytes(id);
return ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=\"file.pdf\"")
.body(fileData);
}
}
record FileMetadata(String id, String filename, Long size) {}
Common Mistakes & Pitfalls
- Using the wrong constant form: Writing produces = MediaType.APPLICATION_JSON (the object) instead of MediaType.APPLICATION_JSON_VALUE (the string). Annotations need compile-time string constants, so you get a compilation error.
- Hardcoding strings: Writing produces = "application/json" everywhere. One typo like "applicaiton/json" breaks the endpoint at runtime with 406 errors. MediaType constants prevent this.
- Not knowing common types: Learners often don't know MediaType.MULTIPART_FORM_DATA_VALUE exists for file uploads or MediaType.TEXT_HTML_VALUE for HTML responses. They hardcode these and wonder why their code looks inconsistent.
- Ignoring isCompatibleWith: When writing custom logic, Learners do string equality checks like mediaType.equals("application/json") instead of using MediaType's built-in compatibility methods that handle wildcards like application slash star.
Check your understanding
Answer these without looking back at the article.
- What is the difference between MediaType.APPLICATION_JSON and MediaType.APPLICATION_JSON_VALUE?
- What MediaType do you use for file uploads in Spring MVC?
- How do you set the Content-Type header on an HttpHeaders object?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.