-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerExceptionHandler.java
More file actions
67 lines (56 loc) · 3.59 KB
/
Copy pathControllerExceptionHandler.java
File metadata and controls
67 lines (56 loc) · 3.59 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
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<RestResponse> argumentErrorHandler(MethodArgumentNotValidException err) {
RestResponse response = new RestResponse(400, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<RestResponse> requestParameterErrorHandler(MissingServletRequestParameterException err) {
RestResponse response = new RestResponse(400, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MissingServletRequestPartException.class)
public ResponseEntity<RestResponse> missingRequestPartHandler(MissingServletRequestPartException err) {
RestResponse response = new RestResponse(404, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
public ResponseEntity<RestResponse> unsatisfiedRequestParameterHandler(UnsatisfiedServletRequestParameterException err) {
RestResponse response = new RestResponse(400, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<RestResponse> methodNotSupportedErrorHandler(HttpRequestMethodNotSupportedException err) {
RestResponse response = new RestResponse(405, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.METHOD_NOT_ALLOWED);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<RestResponse> requestMessageUnreadableHandler(HttpMessageNotReadableException err) {
RestResponse response = new RestResponse(400, Status.FAIL,
err.getMessage(),null);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<RestResponse> mediaTypeNotSupportedErrorHandler(HttpMediaTypeNotSupportedException err) {
RestResponse response = new RestResponse(415, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<RestResponse> noHandlerFoundErrorHandler(NoHandlerFoundException err) {
RestResponse response = new RestResponse(404, Status.FAIL, err.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
}