Performance: Validation error response formatting
MEDIUM IMPACT
This affects the speed of API response delivery and client rendering of error messages.
public ResponseEntity<?> handleValidationErrors(BindingResult result) {
List<ValidationError> errors = result.getFieldErrors().stream()
.map(e -> new ValidationError(e.getField(), e.getDefaultMessage()))
.toList();
ValidationErrorResponse response = new ValidationErrorResponse("Validation Failed", errors);
return ResponseEntity.badRequest().body(response);
}
record ValidationError(String field, String message) {}
record ValidationErrorResponse(String error, List<ValidationError> errors) {}public ResponseEntity<?> handleValidationErrors(BindingResult result) {
Map<String, String> errors = new HashMap<>();
for (FieldError error : result.getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
return ResponseEntity.badRequest().body(errors);
}| Pattern | Server CPU | Response Size | Client Parsing | Verdict |
|---|---|---|---|---|
| Simple Map of Field to Message | Low | Small | High (unstructured parsing) | [!] OK |
| Structured Error Response with Records | Medium (slight overhead) | Small to Medium | Low (easy parsing) | [OK] Good |