Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Spring MVC consumes and produces: 415 and 406 are different failures

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

A Spring MVC mapping can constrain the request Content-Type with consumes and the requested response type with produces.

Download Spring source kit

Two headers, two decisions

The checked controller accepts JSON input and produces JSON output. A matching request returns 200. Sending text/plain as the request body returns 415 Unsupported Media Type. Sending valid JSON while asking only for text/plain returns 406 Not Acceptable. Those statuses answer different client mistakes.

A request body can still be invalid JSON or invalid domain data after its media type matches. Request validation handles that boundary; nested field validation checks inside a deserialized object.

This is a mapping test

MockMvc exercises Spring's mapping and conversion path without a proxy or real network. It does not establish API version negotiation, a maximum request size, charset policy, or safe error bodies for every converter failure. Add explicit integration tests before publishing a compatibility guarantee.

Checked source

Java
@PostMapping(path = "/contract/imports", consumes = "application/json", produces = "application/json")
String importReceipt(@RequestBody String request) {
    return "{"accepted":true}";
}

// MockMvc checks 200 for JSON/JSON, 415 for text/plain input,
// and 406 for an Accept header that permits only text/plain.

Verification boundary

ReceiptMediaTypeTest.separatesRequestTypeFromAcceptableResponseType runs in the downloadable Spring source kit. This excerpt omits imports and test setup; the kit includes complete source.

Costs and boundaries

This test covers one controller and three header combinations. It does not exercise a deployed reverse proxy, streaming upload, body-size limit or versioned representation.

Common Mistakes

  • Do not conflate the request Content-Type with the response Accept header.
  • Do not assume accepted media type means valid JSON or valid receipt data.
  • Do not claim an API versioning policy from a single produces value.

Read next

Spring MVC request validation: reject invalid commands before mutation, Spring MVC ProblemDetail: stable errors without leaking internals, Java HttpClient: request policy, deadlines, and response size.

Continue with checked upload and readiness

Continue with Spring CSV uploads: reject malformed UTF-8 and an unknown header.

spring
spring-boot
receipt-media-types
Storage details