produces Attribute in @GetMapping — Restricting Response Content Type
Fast recall
Quick revision
Core answer
Knowledge of endpoint-level content type constraints and how they interact with Accept headers
Not knowing produces exists or thinking Spring automatically supports all formats without configuration
Show both produces and consumes together on a POST endpoint to demonstrate understanding of bidirectional constraints
Interview-ready guide
Interview answer
For example, @GetMapping(value="/report", produces=MediaType.APPLICATION_JSON_VALUE) matches a client accepting application/json. A client accepting only application/xml receives 406 unless another matching XML-producing method/converter is available. Writing produces="application/json" is equally valid because the annotation property is a String array.
produces Attribute in @GetMapping — Restricting Response Content Type
Direct Answer
The produces attribute declares which response media types a handler method can produce. Spring includes it in request mapping and compares it with the request's Accept preferences during handler selection. If no selected handler can produce an acceptable representation, MVC normally returns 406 Not Acceptable. The value can be a media-type string or a constant whose value is that string.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public Product getProductJson(@PathVariable Long id) {
// Only accepts requests with Accept: application/json
return productService.findById(id);
}
@GetMapping(value = "/{id}/xml", produces = MediaType.APPLICATION_XML_VALUE)
public Product getProductXml(@PathVariable Long id) {
// Only accepts requests with Accept: application/xml
return productService.findById(id);
}
@PostMapping(
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Product> createProduct(@RequestBody Product product) {
// consumes: only accepts JSON request body (checks Content-Type)
// produces: only returns JSON response (checks Accept)
var saved = productService.save(product);
return ResponseEntity.status(HttpStatus.CREATED).body(saved);
}
}
Common Mistakes & Pitfalls
- Using hardcoded strings instead of constants: Writing produces = "application/json" instead of MediaType.APPLICATION_JSON_VALUE. The constant prevents typos and makes refactoring easier.
- Forgetting produces only filters GET/HEAD: For POST endpoints, you need both consumes (for request body) and produces (for response). Freshers often set only produces and wonder why their POST fails with 415 when sending JSON.
- Not understanding 406 vs 415: 406 means the Accept header doesn't match produces. 415 means the Content-Type doesn't match consumes. Freshers see these status codes and think it's a Spring bug instead of a header mismatch.
- Overly restrictive produces on all endpoints: Setting produces on every endpoint when you only return JSON creates maintenance overhead. Use it selectively where you need format constraints.
Check your understanding
Answer these without looking back at the article.
- What is the difference between produces and consumes in Spring MVC?
- What HTTP status is returned when produces doesn't match the Accept header?
- How do you implement content-type-based API versioning in Spring?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.