[spring MVC] Exception(@RestControllerAdvice,@ExceptionHandler)
Categories: Spring
Tags: MVC, Spring, ๊ฐ๋ ์ ๋ฆฌ, ์ค์ต
๐ ๊ฐ์ธ์ ์ธ ๊ณต๊ฐ์ผ๋ก ๊ณต๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ณต์ตํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
์ ํํ์ง ์์ ์ ๋ณด๊ฐ ์์ ์ ์์ผ๋ ์ฐธ๊ณ ๋ฐ๋๋๋ค :๐ธ
[ํ๋ฆฐ ๋ด์ฉ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ณต๋ฐ์ผ์ค๊ฑฐ์์]
์ฐ๋ฆฌ๊ฐ ์์ผ๋ก ๋ง๋ ๋ณผ ์ ์๋ ์์ธ
- ํด๋ผ์ด์ธํธ ์์ฒญ ๋ฐ์ดํฐ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฆ(Validation)์์ ๋ฐ์ํ๋ ์์ธ
- ์๋น์ค ๊ณ์ธต์ ๋น์ฆ๋์ค ๋ก์ง์์ ๋์ ธ์ง๋ ์๋๋ ์์ธ
- ์น ์ ํ๋ฆฌ์ผ์ด์ ์คํ ์ค์ ๋ฐ์ํ๋ ์์ธ(RuntimeException)
์์ธ๋?
- cheked exception
- โ ๊ฐ๋ฐ์๊ฐ ์์ธ์ฒ๋ฆฌ๋ฅผ ๊ผญ ํด์ผํ๋ ๊ฒ ! ์ฆ JVM์ด ์ฝ์ ์ ์๊ฒ ๋ฐ๊ฟ์ฃผ๋ ๊ฒ
@ExceptionHandler
0. ์ด ๋ ๋์ ๋ง๋ค์๋ MemberController class์ ์์ธ์ฒ๋ฆฌ ์ฝ๋ ์ ๋ ฅ
1
2
3
4
5
6
@ExceptionHandler
public ResponseEntity handleException(MethodArgumentNotValidException e){
final List<FieldError>fieldErrors = e.getBindingResult().getFieldErrors();
return new ResponseEntity(fieldErrors,HttpStatus.BAD_REQUEST);
}
-
MethodArgumentNotValidException์ request๋ฅผ ์๋ชป ํ์ ๋ ๋ฐ์ (ํ๋ ๋๋ฝ์ด๋ ๋ฐ๋๊ฐ ํ์์ ๋ง์ง ์๊ฑฐ๋ ์ ํจ์ฑ ๊ฒ์ฆ ์กฐ๊ฑด์ ๋ง์ง ์์ผ๋ฉด ๋ฐ์ํ ์ ์์.)
- ์๋ฅผ ๋ค์ด, DTOํด๋์ค์์
*@NotSpace*(message = "์ปคํผ๋ช (ํ๊ธ)์ ๊ณต๋ฐฑ์ด ์๋์ด์ผ ํฉ๋๋ค.")
๋ฅผ name์์ ์ ์ฉํ๊ณ Controller์ @Valid๋ฅผ ์ ์ฉํ๋๋๋ฐ postman์์ postman์์ Request body์ attruibute์ blank ์์ฑ ํ๊ณ ์์ฒญํ์ ๊ฒฝ์ฐ์ ๋ฐ์ํ ์ ์๋ค.
- ์๋ฅผ ๋ค์ด, DTOํด๋์ค์์
- ๋งค๊ฐ์ธ์๋ก ๋ค์ด์จ
MethodArgumentNotValidException
๊ฐ์ฒด์์getBindingResult().getFieldErrors()
๋ฅผ ํตํด ๋ฐ์ํ ์๋ฌ ์ ๋ณด๋ฅผ ํ์ธ ๊ฐ๋ฅ - ListยซFieldErrorยปfieldErrros์์ FieldError๋?? ์ ๋ฐ ํด๋์ค๋ฅผ ์์ฑํ ๊ฒ์ด ์๋๋ฐ..! ์ฐพ์๋ณด๋
- Spring Framework์์ ์ ๊ณตํ๋ ํด๋์ค๋ผ๊ณ ํจ.
- org.springframework.validation ํจํค์ง์ ์ํด ์์ผ๋ฉฐ, ์ ํจ์ฑ ๊ฒ์ฌ ์ค ๋ฐ์ํ ์ค๋ฅ์ ๋ํ ์ ๋ณด๋ฅผ ํฌํจ
- field: ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํจํ ํ๋์ ์ด๋ฆ.
- rejectedValue: ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํจํ ํ๋์ ๊ฐ
- defaultMessage: ๊ธฐ๋ณธ ์๋ฌ ๋ฉ์์ง
- objectName: ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฐ์ฒด์ ์ด๋ฆ
- codes: ์ค๋ฅ ์ฝ๋๋ฅผ ๋ฐฐ์ด ํํ๋ก ์ ์ฅ
- arguments: ์ค๋ฅ ๋ฉ์์ง์ ์ธ์
- ์์์ ํ์ธํ ์ ๋ณด๋ฅผ
ResponseEntity
๋ฅผ ํตํด Response Body๋ก ์ ๋ฌ -
postman์ ์ด์ฉํด body๋ฅผ ์๋ชป ์ ๋ ฅ ํ post์์ฒญํ๋ฉด ํ๊ธฐ์ ๊ฐ์ด ๋ฉ์ธ์ง๊ฐ ๋ฌ๋ค.
- ์์ ๋ฉ์ธ์ง๋ ๋๋ฌด ๊ธธ๊ณ ํ์ ์๋ ๋ฐ์ดํฐ๊ฐ ๋ง์
1. ๋ฌธ์ ์ ๋ณด์ โ ErrorResponse ํด๋์ค ์ ์ฉ
-
ErrorResponse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import lombok.AllArgsConstructor; import lombok.Getter; import java.util.List; @Getter @AllArgsConstructor public class ErrorResponse { private List<FieldError> fieldErrors; @Getter @AllArgsConstructor public static class FieldError { private String field; private Object rejectedValue; private String reason; } }
FieldError
๋ผ๋ ๋ณ๋์ static class๋ฅผErrorResponse
ํด๋์ค์ ๋ฉค๋ฒ ํด๋์ค๋ก ์ ์(innerclass ๋ผ๊ธฐ๋ณด๋จ ErrorResponse ํด๋์ค์ static ๋ฉค๋ฒ ํด๋์ค๋ผ๊ณ ๋ถ๋ฅด๋ ๊ฒ์ด ์ ์ )
2. MemberController์ handleException() ๋ฉ์๋ ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@ExceptionHandler
public ResponseEntity handleException(MethodArgumentNotValidException e) {
final List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<ErrorResponse.FieldError> errors =
fieldErrors.stream()
.map(error -> new ErrorResponse.FieldError(
error.getField(),
error.getRejectedValue(),
error.getDefaultMessage()))
.collect(Collectors.toList());
return new ResponseEntity<>(new ErrorResponse(errors), HttpStatus.BAD_REQUEST);
}
}
- ์ new ErrorResponse(errors) ์ธ๊ฐ? ๋ชจ๋ฅด๊ฒ์ ์ errors๊ฐ ์๋๋ผ
ErrorResponse.FieldError
ํด๋์ค์ ๋ด์์ List๋ก ๋ณํ ํ,List<ErrorResponse.FieldError>
๋ฅผResponseEntity
ํด๋์ค์ ์ค์ด์ ์ ๋ฌ-
streamd์ด ์๋ ๋ฐ๋ณต๋ฌธ ์ฌ์ฉํด๋ ๋จ.
1 2 3 4 5 6 7 8 9
List<CustomError> errors = new ArrayList<>(); for(FieldError : fieldErrors){ customerError currentError = new CustomError( error.getField(), error.getRejectedValue(), error.getDefaultMessage() ); errors.add(currentError) }
3. @ExceptionHandler์ ๋จ์
- controller๋ controller ์ญํ (์์ฒญ๊ณผ ์๋ต์ ๋ฐ๋!)๋ง ํด์ผํจ - ๊ณ์ธต์ด ํด์ผํ ์ญํ ์ ๋ฒ์ด๋จ
- ๊ฐ Controller ํด๋์ค๋ง๋ค ์ฝ๋ ์ค๋ณต์ด ๋ฐ์
- ํ๋์ ์์ธ๋ง ์๋ ๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ํ๋์ Controller ํด๋์ค ๋ด์์
@ExceptionHandler
๋ฅผ ์ถ๊ฐํ ์๋ฌ ์ฒ๋ฆฌ ํธ๋ค๋ฌ ๋ฉ์๋๊ฐ ์ฆ๊ฐํ๊ฒ ๋จ.
@RestCotrollerAdvice
@RestControllerAdvice - RestController์์ ๋ฐ์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌ
- exception๋ ๊ณตํต๊ด์ฌ์ฌ์ด๊ธฐ ๋๋ฌธ์ member-order-coffee ๋ชจ๋์๊ฒ ์ ์ฉ๋จ.
@RestControllerAdvice
์ ๋ํ ์ด์ ์ ์ถ๊ฐํ ํด๋์ค๋ฅผ ์ด์ฉํ๋ฉด ์์ธ ์ฒ๋ฆฌ๋ฅผ ๊ณตํตํํ ์ ์์- ์ต์์์ advice ํจํค์ง๋ฅผ ๋ง๋ค๊ณ controller์ ์๋ exception์ advice์ ํด๋์ค ์์ฑํ ์ฎ๊ธฐ๋ฉด order, coffee, member ๋ชจ๋ ์์ธ ์ฒ๋ฆฌ๊ฐ ์๋จ.
1. MemberController์ handleException() ๋ฉ์๋ ๋ก์ง ์ญ์
2. ์ต์์ ํจํค์ง์ advice ํจํค์ง ์์ฑ โ ExceptionAdvice ํด๋์ค ์ ์
1
2
3
4
5
6
7
8
package com.springboot.advice;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionAdvice {
}
3. ErrorResponse ์์ - GlobalExceptionAdvice ํด๋์ค์์ ์ฒ๋ฆฌํ Exception ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ๊ตฌํ
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
package com.springboot.response
import lombok.Getter;
import org.springframework.validation.BindingResult;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Getter
public class ErrorResponse {
private List<FieldError> fieldErrors; // (1)
private List<ConstraintViolationError> violationErrors; // (2)
// (3) ์์ฑ์
private ErrorResponse(List<FieldError> fieldErrors, List<ConstraintViolationError> violationErrors) {
this.fieldErrors = fieldErrors;
this.violationErrors = violationErrors;
}
// (4) BindingResult์ ๋ํ ErrorResponse ๊ฐ์ฒด ์์ฑ
public static ErrorResponse of(BindingResult bindingResult) {
return new ErrorResponse(FieldError.of(bindingResult), null);
}
// (5) Set<ConstraintViolation<?>> ๊ฐ์ฒด์ ๋ํ ErrorResponse ๊ฐ์ฒด ์์ฑ
public static ErrorResponse of(Set<ConstraintViolation<?>> violations) {
return new ErrorResponse(null, ConstraintViolationError.of(violations));
}
// (6) Field Error ๊ฐ๊ณต
@Getter
public static class FieldError {
private String field;
private Object rejectedValue;
private String reason;
private FieldError(String field, Object rejectedValue, String reason) {
this.field = field;
this.rejectedValue = rejectedValue;
this.reason = reason;
}
public static List<FieldError> of(BindingResult bindingResult) {
final List<org.springframework.validation.FieldError> fieldErrors =
bindingResult.getFieldErrors();
return fieldErrors.stream()
.map(error -> new FieldError(
error.getField(),
error.getRejectedValue() == null ?
"" : error.getRejectedValue().toString(),
error.getDefaultMessage()))
.collect(Collectors.toList());
}
}
// (7) ConstraintViolation Error ๊ฐ๊ณต
@Getter
public static class ConstraintViolationError {
private String propertyPath;
private Object rejectedValue;
private String reason;
private ConstraintViolationError(String propertyPath, Object rejectedValue,
String reason) {
this.propertyPath = propertyPath;
this.rejectedValue = rejectedValue;
this.reason = reason;
}
public static List<ConstraintViolationError> of(
Set<ConstraintViolation<?>> constraintViolations) {
return constraintViolations.stream()
.map(constraintViolation -> new ConstraintViolationError(
constraintViolation.getPropertyPath().toString(),
constraintViolation.getInvalidValue().toString(),
constraintViolation.getMessage()
)).collect(Collectors.toList());
}
}
}
์ฝ๋๋ ๊ธธ๊ณ ๋๋ฌด ์ด๋ ค์์ ๋นํฉํ๋๋ฐ ์ด๊ฑฐ 2์๊ฐ ๋๊ฒ ๋ณด๊ณ ์ ๋ฆฌํจ..!
- ์ฐ์ ErrorResponse๋ Exception์ ๊ด๋ฆฌํ๋ ํด๋์ค๋ผ๊ณ ๋ณผ ์ ์๊ณ ๋ ๋ฑ๋กํ ์์ธ๊ฐ ์์ผ๋ฉด ์ฌ๊ธฐ์ ์ถ๊ฐํด์ผํจ.
- (1) ๊ณผ (2)์ ์ธ์คํด์ค๋ณ์๋ ๊ฐ๊ฐ static Member class(FieldError์ ConstraintViolationError) ๊ฐ์ฒด์ List๋ฅผ ์ฐธ์กฐ.
- (1) ์
MethodArgumentNotValidException
์ผ๋ก๋ถํฐ ๋ฐ์ํ๋ ์๋ฌ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฉค๋ฒ ๋ณ์, ์ฆ DTO ๋ฉค๋ฒ ๋ณ์ ํ๋์ ์ ํจ์ฑ ๊ฒ์ฆ ์คํจ๋ก ๋ฐ์ํ ์๋ฌ์ ๋ณด๋ฅผ ๋ด์. - (2)๋
ConstraintViolationException
์ผ๋ก๋ถํฐ ๋ฐ์ํ๋ ์๋ฌ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฉค๋ฒ ๋ณ์์ด๊ณ URI ๋ณ์ ๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฆ์ ์คํจ๋ก ๋ฐ์ํ ์๋ฌ์ ๋ณด๋ฅผ ๋ด์
- (1) ์
- (3)์ ErrorResponse์ ์์ฑ์์ธ๋ฐ private๊ฐ ๋ถ์ด ๋ค๋ฅธ ๊ณณ์์๋ ํธ์ถ์ด ๋ถ๊ฐ๋ฅํ๊ณ ํด๋น ํด๋์ค ๋ด์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ (=์บก์ํ), ์ธ๋ถ์์ new๋ก ๊ฐ์ฒด ์์ฑํ ์ ์์
- (4)์ (5)๋ ๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ์ ํด๋น๋จ (๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ์ ๋ฉ์๋๋ช ์ด ๊ฐ๊ณ ๋งค๊ฐ๋ณ์์ ํ์ ์ด ๋ค๋ฅด๋ค)
- (4)์์ of()๋ ๋ฉ์๋ ์ด๋ฆ์ด๋ฉฐ ErrorResponse์ ๊ฐ์ฒด๋ฅผ ์์ฑํจ., bindingResult ๊ฐ ๋ค์ด์ค๋ฉด (3)์ ์คํํ๊ณ ErrorResponse๋ก ๋ฐํ๋จ.
- **BindingResult** ๋ ์คํ๋ง์์ ์ ๊ณตํ๋ ์ ํจ์ฑ๊ฒ์ฌ ์ ๋ณด๋ฅผ ๋ด๋ ๊ฐ์ฒด์ด๋ค. BindingResult ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๋ฌ ์ ๋ณด๋ฅผ ์ถ์ถํ๊ณ ๊ฐ๊ณตํ๋ ์ผ์ ErrorResponse ํด๋์ค์ static ๋ฉค๋ฒ ํด๋์ค์ธ FieldError ํด๋์ค์๊ฒ ์์
- (5)์์๋ of๋ฉ์๋์ Set<ConstraintViolation<?ยปviolations ๊ฐ ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ๋ฉด (3)์ ์คํํ๊ณ ErrorResponse๋ก ๋ฐํ๋จ,
- ConstrainViolation<?>violations์ ?๋ ์ ๋ค๋ฆญ์ ์์ผ๋์นด๋๋ก ๋ค์ํ ํ์ ์ ๋ํ ์ ํจ์ฑ๊ฒ์ฌ๋ฅผ ์งํํ๊ธฐ ์ํจ.
- Set์ ์ฌ์ฉํ ์ด์ ๋ ์ค๋ณต๋ฐฉ์ง์ ๋น ๋ฅธ ์กฐํ๊ฐ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ, Set<ConstraintViolation<?ยป ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๋ฌ ์ ๋ณด๋ฅผ ์ถ์ถํ๊ณ ๊ฐ๊ณตํ๋ ์ผ์ ConstraintViolationError ํด๋์ค์๊ฒ ์์
- (6)์ FieldError ํด๋์ค๋ ErrorReponse์ static Member ํด๋์ค์ ํด๋นํ๊ณ
- FieldError์ of๋ฉ์๋๋ BindingResult์์ ํ๋ ์๋ฌ๋ค์ ์ถ์ถํ์ฌ FieldError๊ฐ์ฒด๋ก ๋ณํํ๊ณ List๋ก ๋ฐํ
- (7)์ ConstraintViolationError๋ ErrorReponse์ static Member ํด๋์ค์ ํด๋นํ๊ณ
- ConstraintViolationError์ of๋ฉ์๋๋ constraintViolation(์ ํจ์ฑ๊ฒ์ฌ์์ ๋ฐ์ํ ์๋ฐ์ฌํญ์ ๋ชจ์๋ ์งํฉ)์ ConstraintViolationError๊ฐ์ฒด๋ก ๋ณํํ๊ณ ๋ฆฌ์คํธ๋ก ๋ฐํํจ.
ConstrainViolation, BindingResult, FieldError ๋ญ๊ฐ ๋ค๋ฅธ์ง ์ ์ดํด๊ฐ ์๊ฐ์ ๋น๊ตํด๋ด.
์ ๋ถ ์ ํจ์ฑ ๊ฒ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ ๊ฐ์ฒด์ธ ๊ฒ ๊ฐ์๋ฐโฆ.๋ค๋ฅธ ์ญํ ์ ํ๋ค๊ณ ํจ.
MethodArgumentNotValidException
@Valid ์ ๋ํ ์ด์ ์ด ๋ฌ๋ฆฐ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ์ ๋ํ ๊ฒ์ฆ์ ์คํจํ์ ๊ฒฝ์ฐ ์๊ธฐ๋ ์์ธ ( BindExcpetion์ ์์๋ฐ์) https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/MethodArgumentNotValidException.html
ConstraintViolationException
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด์ด ์๋ฐ๋ ๋ ๋ฐ์ํ๋ ์์ธ์ ๋๋ค. ์ด๋ฅผ ์ ์ ํ ์ฒ๋ฆฌํ๊ธฐ ์ํด์๋ ์์ธ๋ฅผ ์ก์ ๋กค๋ฐฑํ๊ณ ์ฌ์ฉ์์๊ฒ ์๋ง์ ๋ฉ์์ง๋ฅผ ์ ๊ณตํ๊ฑฐ๋ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๋ ๋ฑ์ ์กฐ์น๊ฐ ํ์ https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintViolationException.html
ย | ConstrainViolation | BindingResult | FieldError |
---|---|---|---|
์ญํ | Bean Validation API์์ ์ ์ฝ ์กฐ๊ฑด์ ์๋ฐํ์ ๋ ๋ฐ์ํ๋ ๊ฐ์ฒด. | Spring MVC์์ ํผ ๋ฐ์ดํฐ์ ์ ํจ์ฑ ๊ฒ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ ๊ฐ์ฒด. | BindingResult ๊ฐ์ฒด ๋ด์์ ํน์ ํ๋์ ์ ํจ์ฑ ๊ฒ์ฌ ์ค๋ฅ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด. |
์ฌ์ฉ | ์ฃผ๋ก Java Bean Validation (javax.validation)์์ ์ฌ์ฉ. | Spring MVC ์ปจํธ๋กค๋ฌ์์ @Valid์ ํจ๊ป ์ฌ์ฉ | BindingResult ๋ด๋ถ์์ ํ๋ ๋ณ ์ ํจ์ฑ ๊ฒ์ฌ ์ค๋ฅ๋ฅผ ๊ด๋ฆฌ. |
์ฃผ์์ ๋ณด | ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํจํ ๊ฐ์ฒด (getRootBean()). | ์ ํจ์ฑ ๊ฒ์ฌ ๊ฒฐ๊ณผ์ ์ค๋ฅ ๋ชฉ๋ก (getFieldErrors()). | ์ค๋ฅ๊ฐ ๋ฐ์ํ ํ๋ ์ด๋ฆ (getField()). |
ย | ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํจํ ๊ฐ (getInvalidValue()). | ํน์ ํ๋์ ์ค๋ฅ ์ฌ๋ถ (hasFieldErrors()). | ์ค๋ฅ ๋ฉ์์ง (getDefaultMessage()). |
ย | ์๋ฐ๋ ์ ์ฝ ์กฐ๊ฑด์ ๋ฉ์์ง (getMessage()). | ์ ์ฒด ์ค๋ฅ ์ฌ๋ถ (hasErrors()). | ๊ฑฐ๋ถ๋ ๊ฐ (getRejectedValue()). |
ย | ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํจํ ์์ฑ ๊ฒฝ๋ก (getPropertyPath()). | ย | ย |
4. Exception ํธ๋ค๋ฌ ๋ฉ์๋ ์์ (GlobalExceptionAdvice)
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
package com.springboot.advice;
import com.springboot.response.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
@RestControllerAdvice
public class GlobalExceptionAdvice {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleMethodArgumentNotValidException(
MethodArgumentNotValidException e) {
final ErrorResponse response = ErrorResponse.of(e.getBindingResult());
return response;
}
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleConstraintViolationException(
ConstraintViolationException e) {
final ErrorResponse response = ErrorResponse.of(e.getConstraintViolations());
return response;
}
}
Comment
exception๋ถํฐ ์ด๋ ต๋ค๊ณ ํด์
์ฝ๋ ๋ฏธ๋ฆฌ ๋ณด๊ณ ๊ฐ๋๋ฐ ๋์ถฉ๋ณด๊ณ ๋ณ๊ฑฐ์๋ ๊ฒ ๊ฐ์๋ฐ
ํ๊ณ ๋ค๋ฅธ ๊ฑฐ ํ๊ณ ์์๋๋ฐ ๋๋ฌด ๋์ถฉ๋ดค๋๋ณด๋คโฆ.!
์ฌ์ค ๋ค๋ฅธ๋ฐ์ try-catch์ฐ๊ธธ๋ ๊ทธ๋ฅ ์์ ์ ํ๋๊ฑฐ๋ค ํ๋๋ฐ
๋ ์ฌํ๋ ๋ด์ฉ์ด ๋์ฌ์ค์ด์ผ..
์ด๋ ๊ฒ ์ด๋ ค์ธ์ค์ ๋ชฐ๋๋๋ฐ of๋์์ ํ๋ฒ ๋นํฉํ๊ณ ์์ฑ์์ private..?
๊ทธ๋ ๊ทธ๊ฑด ์ค์ผ์ด
๊ทผ๋ฐ BindingResult..? ConstraintVoilation? ์ด๊ฒ๋ ๋ค ์ฐพ์๋ด์ผํ๊ณ
ํด๋์ค ์์ ์ด๋ ํด๋์ค๊ฐ ๋ช๊ฐ๋ ์๋์ง. ๊ทผ๋ฐ ์ด๊ฑด ๋ ์ด๋ํด๋์ค๋ผ๊ธฐ๋ณด๋ค
reponseํด๋์ค์ static Member class ๋ผ๊ณ ํ๋ค!
์ผ๋จ ์ ๋ฆฌ๋ ํ๋๋ฐ ๋ฐ๋ง ์ดํด๊ฐ๋ค. ์ดํด๊ฐ ๊ฐ๋ฉด ๋ญํ๋ ์ฝ๋๋ฅผ ์ธ์์ผ ํ๋???์ถ์..
์์ฃผ๋ณด๋ฉด ์ธ ์ ์๊ฒ ์ง! ใ
ใ
ใ
,,,,,,
๐ ๊ณต์ง
Leave a comment