Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8db6225

Browse files
[fix] aop日志
1 parent de1eeab commit 8db6225

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

‎web/src/main/java/demo/web/config/aop/LogAspect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class LogAspect {
3030
/**
3131
* 仅针对{@link RestfulController#hello()} 演示
3232
*/
33-
//@Pointcut("execution(public * demo.web.controller.RestfulController..*(..))")//类内所有方法
33+
//@Pointcut("execution(public * demo.web.controller.RestfulController.*(..))")//类内所有方法
3434
@Pointcut("execution(public * demo.web.controller.RestfulController.hello())")//仅hello()方法
3535
public void webLog() {
3636
}
@@ -55,6 +55,6 @@ public void deAfter(JoinPoint joinPoint) {
5555
list.add(obj);
5656
}
5757
//{} 表示占位符
58-
log.info("method=[{}.{}], cost=[{}ms], args={}", className, methodName, cost, list);
58+
log.info("method=[{}.{}], args={}, cost=[{}ms]", className, methodName, list, cost);
5959
}
6060
}

‎web/src/main/java/demo/web/handler/GlobalExceptionHandler.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package demo.web.handler;
22

3+
import demo.web.controller.base.ResponseCode;
4+
import demo.web.controller.base.ResponseData;
35
import lombok.extern.slf4j.Slf4j;
46
import org.springframework.validation.BindException;
57
import org.springframework.validation.FieldError;
@@ -13,8 +15,6 @@
1315
import javax.servlet.http.HttpServletResponse;
1416
import javax.servlet.http.HttpSession;
1517
import javax.validation.ConstraintViolationException;
16-
import java.util.HashMap;
17-
import java.util.Map;
1818

1919

2020
/**
@@ -36,32 +36,36 @@ public class GlobalExceptionHandler {
3636
* 处理参数校验异常
3737
*/
3838
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
39-
private Map<String, Object> paramHandler(Exception e) {
40-
Map<String, Object> map = new HashMap<>();
41-
FieldError error = new FieldError("obj", "field", "default msg");
39+
private ResponseData paramHandler(Exception e) {
40+
ResponseData responseData = new ResponseData();
41+
responseData.setCode(ResponseCode.PARAM_INVALID);
42+
FieldError error;// = new FieldError("obj", "field", "default msg");
43+
//入参为对象
4244
if (e instanceof MethodArgumentNotValidException) {
4345
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
44-
error = ex.getBindingResult().getFieldError();//如果错误不知一个,亲测好象是随机获取一个错误
45-
} else if (e instanceof BindException) {
46+
error = ex.getBindingResult().getFieldError();//如果错误不止一个,亲测好象是随机获取一个错误
47+
} else {// if (e instanceof BindException) {
48+
//入参为键值对
4649
BindException ex = (BindException) e;
4750
error = ex.getBindingResult().getFieldError();
4851
}
49-
map.put("msg", error.getField() + error.getDefaultMessage() + ":" + error.getRejectedValue());
50-
map.put("type", e.getClass().getName());
51-
return map;
52+
responseData.setMsg(error.getField() + error.getDefaultMessage() + ":" + error.getRejectedValue());
53+
responseData.setExt(e.getClass().getName());
54+
return responseData;
5255
}
5356

5457
/**
5558
* 单个参数校验
5659
*/
5760
@ExceptionHandler(value = ConstraintViolationException.class)
5861
@ResponseBody
59-
public Map<String, Object> handleBindGetException(ConstraintViolationException ex) {
60-
log.error("单个参数校验异常", ex);
61-
Map<String, Object> map = new HashMap<>();
62-
map.put("type", ex.getClass().getName());
63-
map.put("msg", ex.getMessage());
64-
return map;
62+
public ResponseData handleBindGetException(ConstraintViolationException e) {
63+
log.error("单个参数校验异常", e);
64+
ResponseData responseData = new ResponseData();
65+
responseData.setCode(ResponseCode.PARAM_INVALID)
66+
.setMsg(e.getMessage())
67+
.setExt(e.getClass().getName());
68+
return responseData;
6569
}
6670

6771

@@ -70,27 +74,25 @@ public Map<String, Object> handleBindGetException(ConstraintViolationException e
7074
*/
7175
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
7276
@ResponseBody
73-
public Map<String, Object> handleMax(MaxUploadSizeExceededException ex) {
74-
log.error("文件过大", ex);
75-
Map<String, Object> map= new HashMap<>();
76-
map.put("type", ex.getClass().getName());
77-
map.put("msg", ex.getMessage());
78-
map.put("desc", "文件过大");
79-
return map;
77+
public ResponseDatahandleMax(MaxUploadSizeExceededException e) {
78+
log.error("文件过大", e);
79+
ResponseDataresponseData= new ResponseData();
80+
responseData.setCode(ResponseCode.PARAM_INVALID)
81+
.setMsg("上传文件过大")
82+
.setExt(e.getClass().getName());
83+
return responseData;
8084
}
8185

8286
/**
8387
* 默认异常处理,入参需要哪些参数可根据需求而定
8488
*/
8589
@ExceptionHandler(value = Exception.class)
86-
private Map<String, Object> defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp,
87-
HttpSession session, Exception e) {
90+
private ResponseData defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp,
91+
HttpSession session, Exception e) {
8892
log.error("exception handler: ", e);
89-
Map<String, Object> map = new HashMap<>();
90-
map.put("code", 1);
91-
map.put("msg", e.getMessage());
92-
map.put("type", e.getClass().getName());
93-
return map;
93+
ResponseData responseData = ResponseData.exceptionObj(e);
94+
responseData.setExt(e.getClass().getName());
95+
return responseData;
9496
}
9597

9698

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /