-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[Chapter 11] 유효성 검사
120 lines (89 loc) · 3.9 KB
/
[Chapter 11] 유효성 검사
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
### 81. 유효성검사 자동화-AOP처리 1탄
AOP(Aspect Orientied Programming) 처리
- 유효성 검사 AOP 처리
com.cos.photogramstart.handler.aop.ValidationAdvice.java
```java
package com.cos.photogramstart.handler.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component // RestController, Service 모든 것들이 Component를 상속해서 만들어져 있음.
@Aspect
public class ValidationAdvice {
@Around("execution(* com.cos.photogramstart.web.api.*Controller.*(..))")
public Object apiAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("web api 컨트롤러====================");
// proceedingJoinPoint => profile 함수의 모든 곳에 접근할 수 있는 변수
// profile 함수보다 먼저 실행
return proceedingJoinPoint.proceed(); // profile 함수가 실행됨.
}
@Around("execution(* com.cos.photogramstart.web.*Controller.*(..))")
public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("web 컨트롤러==================");
return proceedingJoinPoint.proceed();
}
}
```
### 82. 유효성검사 자동화-AOP처리 2탄
ValidationAdvice.java
```java
package com.cos.photogramstart.handler.aop;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import com.cos.photogramstart.handler.ex.CustomValidationApiException;
import com.cos.photogramstart.handler.ex.CustomValidationException;
@Component // RestController, Service 모든 것들이 Component를 상속해서 만들어져 있음.
@Aspect
public class ValidationAdvice {
@Around("execution(* com.cos.photogramstart.web.api.*Controller.*(..))")
public Object apiAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// System.out.println("web api 컨트롤러====================");
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof BindingResult) {
// System.out.println("유효성 하는 함수입니다.");
BindingResult bindingResult = (BindingResult) arg;
if (bindingResult.hasErrors()) {
Map<String, String> errorMap = new HashMap<>();
for (FieldError error : bindingResult.getFieldErrors()) {
errorMap.put(error.getField(), error.getDefaultMessage());
// System.out.println(error.getDefaultMessage());
}
throw new CustomValidationApiException("유효성 검사 실패함", errorMap);
}
}
}
// proceedingJoinPoint => profile 함수의 모든 곳에 접근할 수 있는 변수
// profile 함수보다 먼저 실행
return proceedingJoinPoint.proceed(); // profile 함수가 실행됨.
}
@Around("execution(* com.cos.photogramstart.web.*Controller.*(..))")
public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// System.out.println("web 컨트롤러==================");
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof BindingResult) {
// System.out.println("유효성 하는 함수입니다.");
BindingResult bindingResult = (BindingResult) arg;
if (bindingResult.hasErrors()) {
Map<String, String> errorMap = new HashMap<>();
for (FieldError error : bindingResult.getFieldErrors()) {
errorMap.put(error.getField(), error.getDefaultMessage());
// System.out.println(error.getDefaultMessage());
}
throw new CustomValidationException("유효성 검사 실패함", errorMap);
}
}
}
return proceedingJoinPoint.proceed();
}
}
```
controller랑 apiController 유효성 검사 삭제