HttpMediaTypeNotSupportedException in Spring Boot

Spring Boot applications leverage the Spring Web module to create robust and scalable web applications. However, when working with web requests, it's possible to encounter certain exceptions. One such exception is the HttpMediaTypeNotSupportedException

What is HttpMediaTypeNotSupportedException? 

HttpMediaTypeNotSupportedException is thrown when the client sends a request with a media type that is not supported by the application. This can happen, for instance, if your controller endpoint is expecting a request with content type application/json, but the client sends a request with content type text/xml

Here is the Exception Trace:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/xml' not supported
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(
    ... more

How to reproduce? 

Let's consider a simple Spring Boot controller:

public class GreetingController {

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> greet(@RequestBody String name) {
        return ResponseEntity.ok("Hello, " + name);


Now, if you send a POST request to /api/greetings with the header Content-Type: text/plain, you'll encounter the exception.

Exception in the console:


Specify Supported Media Types

Make it clear in your controller which media types are supported using the consumes attribute:

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)

Handle the Exception

Use Spring's @ExceptionHandler to catch the exception and provide a meaningful response:

public ResponseEntity<String> handleTypeNotSupported(HttpMediaTypeNotSupportedException e) {
    return ResponseEntity.badRequest().body("Content type not supported.");

Client Awareness

Ensure clients are aware of the expected content type. This often means updating API documentation or providing clear error messages for developers. 

Use Spring's ContentNegotiationConfigurer

You can further customize how media types are resolved using this in a configuration class.


Handling media types correctly is crucial in a web application to ensure seamless client-server interactions. The HttpMediaTypeNotSupportedException gives developers a clear signal that there's a mismatch, and handling it gracefully can make the development process smoother.

Related Spring Exceptions Posts