The produces attribute of @RequestMapping (and of @GetMapping, @PostMapping and the other composed annotations) declares which response media types a handler method can generate. Spring MVC compares it with the request's Accept header and only selects the method when they are compatible. It also sets the Content-Type of the response, and it lets you have several methods on the same URL that return different formats.
If no method on the URL can produce a type the client accepts, Spring answers with HTTP 406 Not Acceptable.
When a request arrives, Spring MVC looks at the Accept header and checks it against each candidate mapping's produces value. A match narrows the choice of handler and fixes the response media type.
A request with Accept: application/json reaches this method and receives Content-Type: application/json. A request with Accept: application/xml finds no matching handler and gets a 406 response.
Use the constants on MediaType, such as MediaType.APPLICATION_JSON_VALUE, instead of raw strings to avoid typos.
Because produces takes part in request matching, two methods can share a path and differ only in the format they return.
Each call reaches a different method. Spring picks the best match using the quality values (q=) in the Accept header.
produces accepts an array. List every type the method can generate.
XML output needs an XML converter on the classpath, for example jackson-dataformat-xml. You can also negate a type with !, as in produces = "!text/plain", to match any request except those that accept only plain text. Negation is rarely needed.
Add a charset parameter when it matters, for example produces = "text/plain;charset=UTF-8". For application/json, UTF-8 is the default and no charset is needed. Spring does not append a charset to JSON responses.
When you set produces on a controller class, every handler inherits it. A method-level produces replaces the class-level value rather than adding to it.
Here /api/v1/status produces JSON and /api/v1/export produces CSV, because the method-level value overrides the class-level one.
| Attribute | Matches against | Controls |
|---|---|---|
produces | Request Accept header | Response media type |
consumes | Request Content-Type header | Request body format the method accepts |
A method that accepts JSON and returns JSON can set both:
A request with an unsupported Content-Type gets 415 Unsupported Media Type, while an unsupported Accept gets 406.
Accept: application/json. Browsers usually send text/html first, so a JSON-only endpoint can return 406. Test with curl or a REST client.produces to a type no HttpMessageConverter can write for your return type.ResponseEntity with a different Content-Type than produces. Keep them consistent.Accept header.Spring MVC cannot find a matching handler and returns 406 Not Acceptable.
No. A @RestController method that returns an object already produces JSON by default when Jackson is on the classpath. Use produces when you need to restrict or choose between formats.
Yes. When the method matches, the response Content-Type is the matched media type from produces.
Yes. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping all have a produces attribute that works like the one on @RequestMapping.
The produces attribute narrows handler selection by the Accept header and sets the response media type. Use it to serve several formats from one URL, to document what an endpoint returns, and to reject unsupported formats with 406. Pair it with consumes for request bodies, and test with a client that sends the Accept header you expect. See the Spring Framework reference on producible media types for more.