-
Notifications
You must be signed in to change notification settings - Fork 19
/
BindingResultAop.java
50 lines (42 loc) · 1.51 KB
/
BindingResultAop.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
package com.lccf.aop;
import com.lccf.util.ResponseVoUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
/**
* 参数验证处理类
* @author lichangchao
*/
@Aspect
@Component
public class BindingResultAop {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(* com.lccf..*.*(..))")
public void aopMethod() {
}
@Around("aopMethod()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.info("before method invoking!");
BindingResult bindingResult = null;
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof MethodArgumentNotValidException) {
bindingResult = ((MethodArgumentNotValidException) arg).getBindingResult();
}
}
if (bindingResult != null) {
if (bindingResult.hasErrors()) {
String errorInfo = bindingResult.getFieldError().getDefaultMessage();
return ResponseVoUtil.failResult(errorInfo);
}
}
return joinPoint.proceed();
}
}