Performance: Content type negotiation
MEDIUM IMPACT
Content type negotiation affects how quickly the server can respond with the correct data format, impacting initial response time and perceived page load speed.
@GetMapping(value = "/data", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}) public ResponseEntity<?> getData() { // Spring automatically negotiates content type return ResponseEntity.ok(dataObject); }
public ResponseEntity<String> getData(HttpServletRequest request) {
String accept = request.getHeader("Accept");
if (accept != null && accept.contains("application/json")) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(jsonData);
} else if (accept != null && accept.contains("application/xml")) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(xmlData);
} else {
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).build();
}
}| Pattern | CPU Usage | Response Time | Error Risk | Verdict |
|---|---|---|---|---|
| Manual header parsing and response selection | High (extra CPU cycles) | Higher (~10-20ms slower) | Higher (manual errors possible) | [X] Bad |
| Spring built-in content negotiation | Low (optimized internal handling) | Lower (faster response) | Low (framework tested) | [OK] Good |