ResBodyAdvice.java
3.12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package vion.advice;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import vion.utils.ResultUtil;
import vion.vo.ResultVO;
/**
* @author HlQ
* @date 2023/11/2
*/
@Component
@RestControllerAdvice("vion.controller")
@Slf4j
public class ResBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return !returnType.getMethod().getReturnType().isAssignableFrom(Void.TYPE);
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof ResultVO || body instanceof ModelAndView) {
return body;
}
ResultVO resultVO = ResultUtil.success(body);
if (returnType.getParameterType().isAssignableFrom(String.class)) {
// 字符串类型特殊处理
try {
return new ObjectMapper().writeValueAsString(resultVO);
} catch (JsonProcessingException e) {
}
}
return resultVO;
}
@ExceptionHandler(Exception.class)
public ResultVO exceptionHandler(Exception e) {
log.error("接口调用出错", e);
return ResultUtil.error(e.getMessage());
}
@ExceptionHandler(IllegalArgumentException.class)
public ResultVO argExceptionHandler(Exception e) {
log.error("参数异常", e);
return ResultUtil.error(e.getMessage());
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResultVO methodArgExceptionHandler(Exception e) {
log.error("方法参数异常", e);
return ResultUtil.error("Method parameter error.");
}
@ExceptionHandler(NotLoginException.class)
public ResultVO notLoginExceptionHandler(Exception e) {
log.error("未登录", e.getMessage());
return ResultUtil.error(401, "请重新登录");
}
@ExceptionHandler(NotPermissionException.class)
public ResultVO notPermissionExceptionHandler(NotPermissionException e) {
log.error("无权限", e.getMessage());
return ResultUtil.error(500, "缺少权限:" + e.getPermission());
}
}