-
Notifications
You must be signed in to change notification settings - Fork 46
/
BaseEndpoint.java
112 lines (87 loc) · 4.38 KB
/
BaseEndpoint.java
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
package com.droidablebee.springboot.rest.endpoint;
import com.droidablebee.springboot.rest.endpoint.error.Error;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseEndpoint {
@Autowired
protected MessageSource messageSource;
@ExceptionHandler
protected ResponseEntity<?> handleBindException(BindException exception) {
return ResponseEntity.badRequest().body(convert(exception.getAllErrors()));
}
/**
* Exception handler for validation errors caused by method parameters @RequesParam, @PathVariable, @RequestHeader annotated with jakarta.validation constraints.
*/
@ExceptionHandler
protected ResponseEntity<?> handleConstraintViolationException(ConstraintViolationException exception) {
List<Error> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : exception.getConstraintViolations()) {
String value = (violation.getInvalidValue() == null ? null : violation.getInvalidValue().toString());
errors.add(new Error(violation.getPropertyPath().toString(), value, violation.getMessage()));
}
return ResponseEntity.badRequest().body(errors);
}
/**
* Exception handler for @RequestBody validation errors.
*/
@ExceptionHandler
protected ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) {
return ResponseEntity.badRequest().body(convert(exception.getBindingResult().getAllErrors()));
}
/**
* Exception handler for missing required parameters errors.
*/
@ExceptionHandler
protected ResponseEntity<?> handleServletRequestBindingException(ServletRequestBindingException exception) {
return ResponseEntity.badRequest().body(new Error(null, null, exception.getMessage()));
}
/**
* Exception handler for invalid payload (e.g. json invalid format error).
*/
@ExceptionHandler
protected ResponseEntity<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) {
return ResponseEntity.badRequest().body(new Error(null, null, exception.getMessage()));
}
@ExceptionHandler
protected ResponseEntity<?> handleAccessDeniedException(AccessDeniedException exception) {
return new ResponseEntity<>(new Error(null, null, exception.getMessage()), HttpStatus.FORBIDDEN);
}
@ExceptionHandler
protected ResponseEntity<?> handleException(Exception exception) {
return new ResponseEntity<>(new Error(null, null, exception.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
protected List<Error> convert(List<ObjectError> objectErrors) {
List<Error> errors = new ArrayList<>();
for (ObjectError objectError : objectErrors) {
String message = objectError.getDefaultMessage();
if (message == null) {
//when using custom spring validator org.springframework.validation.Validator need to resolve messages manually
message = messageSource.getMessage(objectError, null);
}
Error error;
if (objectError instanceof FieldError) {
FieldError fieldError = (FieldError) objectError;
String value = (fieldError.getRejectedValue() == null ? null : fieldError.getRejectedValue().toString());
error = new Error(fieldError.getField(), value, message);
} else {
error = new Error(objectError.getObjectName(), objectError.getCode(), objectError.getDefaultMessage());
}
errors.add(error);
}
return errors;
}
}