In JAX-RS, now called Jakarta RESTful Web Services, @Consumes declares which request media types a resource method accepts, and @Produces declares which response media types it can return. The runtime matches them against the client's Content-Type and Accept headers, so the same URL can serve JSON to one client and XML to another, and reject formats it cannot handle.
Since Jakarta EE 9 the annotations live in jakarta.ws.rs, not javax.ws.rs. Older code and tutorials still use the javax package.
Two request headers drive the matching:
| Header | Sent by | Matched against | Failure status |
|---|---|---|---|
Content-Type | Client, describing the request body | @Consumes | 415 Unsupported Media Type |
Accept | Client, listing acceptable responses | @Produces | 406 Not Acceptable |
If a method has no annotation, it accepts or produces any media type, and the runtime picks a default. Being explicit is better, because it documents the API and gives clients the right error codes.
@Produces takes one or more media types. Use the MediaType constants to avoid typos.
A request with Accept: application/json returns the JSON body. A request with Accept: application/xml gets a 406 response, because the method does not produce XML.
Common constants are APPLICATION_JSON, APPLICATION_XML, TEXT_PLAIN, TEXT_HTML and APPLICATION_FORM_URLENCODED. Custom types such as application/vnd.myapp.v1+json are plain strings, which is the usual way to version an API by media type.
@Consumes applies to methods that read a request body, typically POST, PUT and PATCH.
If a client sends Content-Type: text/plain to this method, the runtime answers 415 without invoking your code.
Placing the annotations on the class sets a default for every method. A method-level annotation overrides the class-level one for that method.
Both annotations accept arrays, and the client can rank its preferences with quality values:
With Accept: application/xml;q=0.9, application/json;q=0.5 the runtime picks XML because it has the higher q value. You can also set a server-side preference, such as application/json;qs=1.0. A wildcard such as */* or application/* matches broadly.
The annotations only select a media type. Converting Item to JSON is done by a MessageBodyWriter, and reading it back is done by a MessageBodyReader. Implementations such as Jersey, RESTEasy and Quarkus REST ship JSON support through Jackson or JSON-B modules. If no provider supports the type and media type pair, the call fails with a 500 or 415 error, so check that the JSON module is on the classpath.
@Consumes filters on the request body type (Content-Type), and @Produces filters on the response type the client accepts (Accept). One describes input, the other output.
The method accepts and produces any media type, and the runtime chooses the format based on the return type or entity. This often works, but it makes the API contract implicit.
415 Unsupported Media Type when the request Content-Type does not match @Consumes, and 406 Not Acceptable when the Accept header cannot be satisfied by @Produces.
Yes. @RequestMapping and its shortcuts take consumes and produces attributes, for example @PostMapping(value = "/items", consumes = "application/json", produces = "application/json").
@Consumes and @Produces declare the media types a Jakarta REST method reads and writes, and the runtime uses them for content negotiation. Set them on the class for defaults, override them per method, and rely on the 415 and 406 responses for mismatches. Use the jakarta.ws.rs package on current platforms, and make sure a JSON provider is available.