This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path302-frameworks-spring-boot-rest.mdc
More file actions
765 lines (636 loc) · 31.3 KB
/
302-frameworks-spring-boot-rest.mdc
File metadata and controls
765 lines (636 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
---
description:
globs:
alwaysApply: false
---
# Java REST API Design Principles
This comprehensive guide provides essential principles for designing robust, maintainable, and secure REST APIs using Spring Boot. These rules ensure your APIs follow industry best practices, maintain consistency, and provide excellent developer experience for API consumers.
## Implementing These Principles
These guidelines are built upon the following core principles:
- **Semantic Consistency**: Use HTTP methods, status codes, and URI patterns according to their intended semantics
- **Clear Communication**: Provide unambiguous API contracts through proper DTOs, error handling, and documentation
- **Security by Design**: Implement authentication, authorization, and input validation from the start
- **Evolutionary Design**: Version APIs and structure them to support future changes without breaking existing clients
## Table of contents
- Rule 1: Use HTTP Methods Correctly
- Rule 2: Design Clear and Consistent Resource URIs
- Rule 3: Use HTTP Status Codes Appropriately
- Rule 4: Implement Effective Request and Response Payloads (DTOs)
- Rule 5: Version Your APIs
- Rule 6: Handle Errors Gracefully
- Rule 7: Secure Your APIs
- Rule 8: Document Your APIs
- Rule 9: Use Controller Advice for Global Exception Handling
- Rule 10: Implement Problem Details for Error Responses
## Rule 1: Use HTTP Methods Correctly
Title: Employ HTTP Methods Semantically
Description: Use HTTP methods according to their defined semantics to ensure predictability and compliance with web standards. `GET` for retrieval, `POST` for creation, `PUT` for update/replace, `PATCH` for partial update, and `DELETE` for removal.
**Good example:**
```java
// Using Spring MVC annotations for illustration
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}") // GET for retrieving a user
public ResponseEntity<UserDTO> getUser(@PathVariable String id) {
// ... logic to fetch user ...
return ResponseEntity.ok(new UserDTO());
}
@PostMapping // POST for creating a new user
public ResponseEntity<UserDTO> createUser(@RequestBody UserCreateDTO userCreateDTO) {
// ... logic to create user ...
UserDTO newUser = new UserDTO(); // Assume it gets an ID after creation
return ResponseEntity.created(URI.create("/users/" + newUser.getId())).body(newUser);
}
@PutMapping("/{id}") // PUT for replacing/updating a user
public ResponseEntity<UserDTO> updateUser(@PathVariable String id, @RequestBody UserUpdateDTO userUpdateDTO) {
// ... logic to update user ...
return ResponseEntity.ok(new UserDTO());
}
@DeleteMapping("/{id}") // DELETE for removing a user
public ResponseEntity<Void> deleteUser(@PathVariable String id) {
// ... logic to delete user ...
return ResponseEntity.noContent().build();
}
@PatchMapping("/{id}") // PATCH for partial updates
public ResponseEntity<UserDTO> partiallyUpdateUser(@PathVariable String id, @RequestBody Map<String, Object> updates) {
// ... logic to partially update user ...
return ResponseEntity.ok(new UserDTO());
}
}
// Dummy DTO classes
class UserDTO { private String id; public String getId() { return id; } /* ... other fields, getters, setters ... */ }
class UserCreateDTO { /* ... fields ... */ }
class UserUpdateDTO { /* ... fields ... */ }
```
**Bad Example:**
```java
@RestController
@RequestMapping("/api")
public class BadUserController {
// Bad: Using GET to perform a state change (e.g., delete)
@GetMapping("/deleteUser")
public ResponseEntity<String> deleteUserViaGet(@RequestParam String id) {
System.out.println("Deleting user: " + id + " (Bad: GET used for delete)");
// ... delete logic ...
return ResponseEntity.ok("User deleted (but GET was used!)");
}
// Bad: Using POST for all operations, including retrieval
@PostMapping("/getUser")
public ResponseEntity<UserDTO> getUserViaPost(@RequestBody String idPayload) {
System.out.println("Fetching user: " + idPayload + " (Bad: POST used for GET)");
// ... fetch logic ...
return ResponseEntity.ok(new UserDTO());
}
}
```
## Rule 2: Design Clear and Consistent Resource URIs
Title: Use Nouns for Resources and Maintain URI Consistency
Description: Design URIs that are intuitive and clearly represent resources. Use nouns (e.g., `/users`, `/orders`) instead of verbs. Keep URIs consistent in style (e.g., lowercase, hyphenated or camelCase for path segments).
**Good example:**
```
GET /users // Get all users
GET /users/{userId} // Get a specific user
GET /users/{userId}/orders // Get all orders for a specific user
GET /users/{userId}/orders/{orderId} // Get a specific order for a user
POST /users // Create a new user
```
**Bad Example:**
```
GET /getAllUsers
GET /fetchUserById?id={userId}
POST /createNewUser
GET /userOrders?userId={userId} // Mixing query params and path styles inconsistently
POST /processUserOrderCreation // URI contains verbs and is overly complex
```
## Rule 3: Use HTTP Status Codes Appropriately
Title: Return Meaningful HTTP Status Codes
Description: Utilize standard HTTP status codes to accurately reflect the outcome of API requests. This helps clients understand the result without needing to parse the response body for basic success/failure information.
- `200 OK`: General success.
- `201 Created`: Resource successfully created (often with a `Location` header pointing to the new resource).
- `204 No Content`: Success, but no content to return (e.g., after a successful `DELETE`).
- `400 Bad Request`: Client error (e.g., invalid syntax, missing parameters).
- `401 Unauthorized`: Authentication is required and has failed or has not yet been provided.
- `403 Forbidden`: Authenticated client does not have permission to access the resource.
- `404 Not Found`: Resource not found.
- `500 Internal Server Error`: A generic error message for unexpected server-side errors.
**Good example:**
```java
// (Inside a Spring @RestController method)
if (resourceNotFound) {
return ResponseEntity.notFound().build(); // 404
}
if (!userHasPermission) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Access denied"); // 403
}
if (validationFailed) {
return ResponseEntity.badRequest().body("Invalid input data"); // 400
}
// For creation:
// return ResponseEntity.created(newResourceUri).body(newResource); // 201
// For successful deletion:
// return ResponseEntity.noContent().build(); // 204
```
**Bad Example:**
```java
import java.util.Objects;
public ResponseEntity<String> processSomething(String input) {
try {
if (Objects.isNull(input)) {
// Client should receive a 400 Bad Request, not 200 with an error message in body.
return ResponseEntity.ok("{\"error\":\"Input cannot be null\"}");
}
// ... process ...
return ResponseEntity.ok("{\"data\":\"Success!\"}");
} catch (Exception e) {
// Client should receive a 500 Internal Server Error, not 200.
return ResponseEntity.ok("{\"error\":\"Something went wrong on the server\"}");
}
}
```
## Rule 4: Implement Effective Request and Response Payloads (DTOs)
Title: Use Data Transfer Objects (DTOs) for Payloads and Keep Them Lean
Description: Use dedicated DTO classes for request and response bodies instead of exposing internal domain/entity objects directly. This decouples your API contract from your internal data model. Keep DTOs focused on the data needed for the specific API operation. Use consistent naming conventions (e.g., JSON with camelCase keys).
**Good example:**
```java
// Domain Entity (internal)
class User {
private Long id;
private String username;
private String passwordHash; // Internal field, should not be in API responses
private String email;
private java.time.LocalDateTime createdAt;
// getters, setters
}
// DTO for API responses (exposes only necessary fields)
class UserResponseDTO {
private Long id;
private String username;
private String email;
// getters, setters
}
// DTO for creating a user
class UserCreateRequestDTO {
private String username;
private String password; // Received in request, then hashed internally
private String email;
// getters, setters
}
// In controller:
// public ResponseEntity<UserResponseDTO> getUser(@PathVariable Long id) { ... }
// public ResponseEntity<UserResponseDTO> createUser(@RequestBody UserCreateRequestDTO createDto) { ... }
```
**Bad Example:**
```java
// Bad: Exposing internal User entity directly in API, including sensitive fields like passwordHash.
@RestController
public class AnotherUserController {
@GetMapping("/rawusers/{id}")
public ResponseEntity<User> getRawUser(@PathVariable String id) {
// Assume User class has passwordHash and other internal fields
User internalUser = findUserById(id); // Method that returns the internal User entity
return ResponseEntity.ok(internalUser); // Exposes passwordHash, createdAt, etc.
}
private User findUserById(String id) { return new User(); /* ... */}
}
```
## Rule 5: Version Your APIs
Title: Implement a Clear API Versioning Strategy
Description: Introduce API versioning from the beginning to manage changes and evolution without breaking existing clients. Common strategies include URI versioning (e.g., `/v1/users`), custom request header versioning (e.g., `X-API-Version: 1`), or media type versioning (e.g., `Accept: application/vnd.example.v1+json`). Choose a strategy and apply it consistently.
**Good example (URI Versioning):**
```java
@RestController
@RequestMapping("/api/v1/products") // Version in URI
public class ProductControllerV1 {
// ... v1 endpoints ...
}
@RestController
@RequestMapping("/api/v2/products") // New version in URI
public class ProductControllerV2 {
// ... v2 endpoints with potential breaking changes or new features ...
}
```
**Bad Example (No Versioning):**
```java
@RestController
@RequestMapping("/products") // No version information
public class UnversionedProductController {
// If a breaking change is made here (e.g., field removed from response),
// all existing clients might break.
@GetMapping("/{id}")
public ProductDTO getProduct(@PathVariable String id) {
// ... logic ...
return new ProductDTO();
}
}
class ProductDTO {}
```
## Rule 6: Handle Errors Gracefully
Title: Provide Clear and Consistent Error Responses
Description: When an error occurs, return a standardized, machine-readable error response format (e.g., JSON). Include a unique error code or type, a human-readable message, and optionally, details about specific fields that caused validation errors. Do not expose sensitive internal details like stack traces in production error responses.
**Good example (Error DTO and @ControllerAdvice for Spring):**
```java
// Error Response DTO
class ErrorResponse {
private String errorCode;
private String message;
private List<String> details; // Optional: for field-specific validation errors
// Constructor, getters
public ErrorResponse(String errorCode, String message) { this.errorCode = errorCode; this.message = message; }
public ErrorResponse(String errorCode, String message, List<String> details) { /* ... */ }
}
@ControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleResourceNotFound(ResourceNotFoundException ex) {
return new ErrorResponse("RESOURCE_NOT_FOUND", ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class) // Example for bean validation errors
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleValidationErrors(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult().getFieldErrors().stream()
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
.collect(Collectors.toList());
return new ErrorResponse("VALIDATION_ERROR", "Input validation failed", errors);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleGenericError(Exception ex) {
// Log the full exception internally
// logger.error("Unhandled exception:", ex);
return new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred.");
}
}
// Custom exception
class ResourceNotFoundException extends RuntimeException { public ResourceNotFoundException(String msg){ super(msg);}}
```
**Bad Example:**
```java
@RestController
public class BadErrorHandlingController {
@GetMapping("/items/{id}")
public ResponseEntity<String> getItem(@PathVariable String id) {
if (id.equals("unknown")) {
// Bad: Returning plain text error, or HTML, or inconsistent format.
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Item not found!");
}
try {
// ... some logic that might throw an exception ...
if(id.equals("fail")) throw new NullPointerException("Simulated internal error");
return ResponseEntity.ok("Item data");
} catch (Exception e) {
// Bad: Exposing stack trace to the client in production.
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.toString());
}
}
}
```
## Rule 7: Secure Your APIs
Title: Implement Robust Security Measures
Description: Protect your APIs from common threats. Use HTTPS for all communication. Implement proper authentication (e.g., OAuth 2.0, JWT) and authorization (e.g., role-based access control). Validate all input data to prevent injection attacks (SQLi, XSS). Apply rate limiting and throttling to prevent abuse.
**Good example (Conceptual - actual implementation involves security frameworks like Spring Security):**
```java
// In a Spring Security configuration:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable() // Consider CSRF protection needs
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN") // Role-based authorization
.anyRequest().authenticated() // All other requests need authentication
.and()
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); // Example: JWT authentication
// .httpBasic(); // Or basic auth for simplicity in some cases
}
// ... user details service, password encoder, etc. ...
}
// In a controller method:
// @PreAuthorize("hasAuthority('SCOPE_read:users')") // Example: OAuth2 scope-based authorization
// @GetMapping("/users/{id}")
// public UserDTO getUser(@PathVariable String id) { ... }
```
**Bad Example:**
```java
@RestController
public class InsecureController {
// Bad: No authentication or authorization for sensitive operations.
@PostMapping("/admin/deleteAllData")
public String deleteAllData() {
System.out.println("All data deleted! (INSECURE - NO AUTH)");
return "All data wiped.";
}
// Bad: Trusting user input directly in a query (conceptual SQLi vulnerability).
@GetMapping("/products")
public List<ProductDTO> searchProducts(@RequestParam String category) {
// String query = "SELECT * FROM products WHERE category = '" + category + "'"; // SQL Injection risk!
// Use PreparedStatement or an ORM to prevent SQLi.
System.out.println("Searching for category (raw input): " + category);
return List.of();
}
}
```
## Rule 8: Document Your APIs
Title: Provide Clear and Comprehensive API Documentation
Description: Maintain up-to-date documentation for your API. Tools like Swagger/OpenAPI can be used to generate interactive documentation from code annotations. Documentation should cover resource URIs, HTTP methods, request/response formats (including DTO schemas), expected status codes, authentication methods, and error responses.
**Good example (Using Springdoc OpenAPI / Swagger annotations):**
```java
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
// Assume other necessary imports like Spring MVC, DTOs etc.
@RestController
@RequestMapping("/api/v1/widgets")
@Tag(name = "Widget API", description = "APIs for managing widgets")
public class WidgetController {
@Operation(summary = "Get a widget by its ID",
description = "Returns a single widget based on the provided ID.",
responses = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved widget",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = WidgetDTO.class))),
@ApiResponse(responseCode = "404", description = "Widget not found",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/{widgetId}")
public ResponseEntity<WidgetDTO> getWidgetById(
@Parameter(description = "ID of the widget to retrieve", required = true)
@PathVariable String widgetId) {
// ... logic ...
// return ResponseEntity.ok(new WidgetDTO(widgetId, "Sample Widget"));
// For example, if not found:
if ("unknown".equals(widgetId)) {
throw new ResourceNotFoundException("Widget with ID " + widgetId + " not found.");
}
return ResponseEntity.ok(new WidgetDTO()); // Placeholder
}
}
class WidgetDTO { /* fields, getters, setters */ }
// ErrorResponse and ResourceNotFoundException as defined in Rule 6
```
**Bad Example (No Documentation or Incomplete):**
```java
// No API documentation tool usage, comments are sparse or missing.
@RestController
@RequestMapping("/legacy/things")
public class LegacyThingController {
// What does this do? What are parameters? What are responses?
@GetMapping("/{id}")
public Object getAThing(@PathVariable String id, @RequestParam(required = false) String type) {
// ... complex logic ...
return new Object(); // Unclear what this object structure is.
}
}
// Clients have to guess or read source code to understand how to use the API.
```
## Rule 9: Use Controller Advice for Global Exception Handling
Title: Implement Centralized Exception Handling with @ControllerAdvice
Description: Use `@ControllerAdvice` to create a centralized exception handling mechanism that can catch and handle both checked `Exception` and unchecked `RuntimeException` across all controllers. Use Spring Boot's built-in `ProblemDetail` class for consistent, standardized error responses that follow RFC 7807. This approach promotes DRY principles, ensures consistent error responses, and separates error handling logic from business logic.
**Good example:**
```java
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ProblemDetail> handleIllegalArgument(
IllegalArgumentException ex, HttpServletRequest request) {
logger.warn("Invalid argument: {}", ex.getMessage());
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, ex.getMessage()
);
problemDetail.setType(URI.create("https://example.com/problems/invalid-argument"));
problemDetail.setTitle("Invalid Argument");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
return ResponseEntity.badRequest().body(problemDetail);
}
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ProblemDetail> handleEntityNotFound(
EntityNotFoundException ex, HttpServletRequest request) {
logger.warn("Entity not found: {}", ex.getMessage());
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage()
);
problemDetail.setType(URI.create("https://example.com/problems/entity-not-found"));
problemDetail.setTitle("Entity Not Found");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(problemDetail);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ProblemDetail> handleRuntimeException(
RuntimeException ex, HttpServletRequest request) {
String errorId = UUID.randomUUID().toString();
logger.error("Unexpected runtime exception with ID: {}", errorId, ex);
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR,
"An unexpected error occurred while processing the request"
);
problemDetail.setType(URI.create("https://example.com/problems/internal-error"));
problemDetail.setTitle("Internal Server Error");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("errorId", errorId);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(problemDetail);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ProblemDetail> handleGenericException(
Exception ex, HttpServletRequest request) {
String errorId = UUID.randomUUID().toString();
logger.error("Unexpected exception with ID: {}", errorId, ex);
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR,
"An unexpected error occurred while processing the request"
);
problemDetail.setType(URI.create("https://example.com/problems/internal-error"));
problemDetail.setTitle("Internal Server Error");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("errorId", errorId);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(problemDetail);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ProblemDetail> handleValidationException(
MethodArgumentNotValidException ex, HttpServletRequest request) {
List<String> errors = ex.getBindingResult().getFieldErrors().stream()
.map(error -> error.getField() + ": " + error.getDefaultMessage())
.collect(Collectors.toList());
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, "Validation failed for the provided input"
);
problemDetail.setType(URI.create("https://example.com/problems/validation-error"));
problemDetail.setTitle("Validation Error");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("violations", errors);
return ResponseEntity.badRequest().body(problemDetail);
}
}
// Custom exceptions
class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message) { super(message); }
}
```
**Bad Example:**
```java
@RestController
public class BadUserController {
// Bad: Exception handling scattered across multiple controllers
@GetMapping("/users/{id}")
public ResponseEntity<?> getUser(@PathVariable String id) {
try {
if (id.equals("invalid")) {
throw new IllegalArgumentException("Invalid user ID");
}
if (id.equals("notfound")) {
throw new EntityNotFoundException("User not found");
}
// ... business logic ...
return ResponseEntity.ok(new UserDTO());
} catch (IllegalArgumentException e) {
// Bad: Inconsistent error format, not using ProblemDetail
return ResponseEntity.badRequest().body("Error: " + e.getMessage());
} catch (EntityNotFoundException e) {
// Bad: Different error format, no error details
return ResponseEntity.notFound().build();
} catch (Exception e) {
// Bad: Exposing stack trace and inconsistent format
return ResponseEntity.status(500).body("Server error: " + e.toString());
}
}
// Bad: Different controller with different exception handling approach
@PostMapping("/users")
public ResponseEntity<?> createUser(@RequestBody UserCreateDTO user) {
try {
// ... business logic ...
return ResponseEntity.ok().build();
} catch (RuntimeException e) {
// Bad: Yet another inconsistent error format, not using ProblemDetail
Map<String, String> error = Map.of("error", e.getMessage());
return ResponseEntity.status(500).body(error);
}
}
// Bad: Using custom error response instead of standard ProblemDetail
@DeleteMapping("/users/{id}")
public ResponseEntity<?> deleteUser(@PathVariable String id) {
try {
// ... business logic ...
return ResponseEntity.noContent().build();
} catch (Exception e) {
// Bad: Custom error format instead of ProblemDetail
CustomErrorResponse error = new CustomErrorResponse(
"DELETE_ERROR", e.getMessage(), new Date()
);
return ResponseEntity.status(500).body(error);
}
}
}
// Bad: Custom error response class instead of using ProblemDetail
class CustomErrorResponse {
private String code;
private String message;
private Date timestamp;
// constructors, getters, setters...
}
```
## Rule 10: Implement Problem Details for Error Responses
Title: Use RFC 7807 Problem Details for HTTP APIs
Description: Implement standardized error responses using RFC 7807 Problem Details format for HTTP 500 (Internal Server Error) and other error responses. This provides machine-readable error information that includes a type, title, status, detail, and instance to help clients understand and handle errors consistently.
**Good example:**
```java
@ControllerAdvice
public class ProblemDetailsExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(ProblemDetailsExceptionHandler.class);
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ProblemDetail> handleRuntimeException(
RuntimeException ex, HttpServletRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR,
"An unexpected error occurred while processing the request"
);
problemDetail.setType(URI.create("https://example.com/problems/internal-error"));
problemDetail.setTitle("Internal Server Error");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("errorId", UUID.randomUUID().toString());
// Log the actual exception for debugging (don't expose to client)
logger.error("Internal server error with ID: {}",
problemDetail.getProperties().get("errorId"), ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(problemDetail);
}
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ProblemDetail> handleEntityNotFound(
EntityNotFoundException ex, HttpServletRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage()
);
problemDetail.setType(URI.create("https://example.com/problems/entity-not-found"));
problemDetail.setTitle("Entity Not Found");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(problemDetail);
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<ProblemDetail> handleValidation(
ValidationException ex, HttpServletRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, "Validation failed for the provided input"
);
problemDetail.setType(URI.create("https://example.com/problems/validation-error"));
problemDetail.setTitle("Validation Error");
problemDetail.setInstance(URI.create(request.getRequestURI()));
problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("violations", ex.getViolations());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
}
}
// Custom validation exception
class ValidationException extends RuntimeException {
private final List<String> violations;
public ValidationException(String message, List<String> violations) {
super(message);
this.violations = violations;
}
public List<String> getViolations() { return violations; }
}
```
**Bad Example:**
```java
@ControllerAdvice
public class BadExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, Object> handleRuntimeException(RuntimeException ex) {
// Bad: Non-standard error format, inconsistent fields
Map<String, Object> error = new HashMap<>();
error.put("error", true);
error.put("msg", "Something went wrong");
error.put("exception_type", ex.getClass().getSimpleName());
error.put("time", new Date());
// Bad: Exposing sensitive stack trace information
error.put("stack_trace", Arrays.toString(ex.getStackTrace()));
return error;
}
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<String> handleNotFound(EntityNotFoundException ex) {
// Bad: Inconsistent response format (string vs JSON vs problem details)
return ResponseEntity.status(404).body("Not found: " + ex.getMessage());
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Object> handleValidation(ValidationException ex) {
// Bad: Yet another inconsistent format
return ResponseEntity.badRequest().body(
Map.of("validationErrors", ex.getViolations())
);
}
// Bad: Missing proper error ID, timestamps, and structured format
// Bad: No type URIs or standard problem details structure
// Bad: Inconsistent error formats across different exception types
}
```