ResBodyAdvice.java
3.2 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
83
package vion.advice;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.util.ObjUtil;
import org.springframework.core.MethodParameter;
import org.springframework.core.io.Resource;
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.JsonUtil;
import vion.utils.ResultUtil;
import vion.vo.ResultVO;
/**
* @author vion
* @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 (ObjUtil.isNull(body)
|| body instanceof ResultVO
|| body instanceof ModelAndView
|| body instanceof Resource
|| "vion.controller.mqtt".equalsIgnoreCase(returnType.getDeclaringClass().getPackageName())) {
return body;
}
var resultVO = ResultUtil.success(body);
if (returnType.getParameterType().isAssignableFrom(String.class)) {
// 字符串类型特殊处理
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return JsonUtil.toJsonString(resultVO);
}
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("方法参数异常");
}
@ExceptionHandler(NotLoginException.class)
public ResultVO notLoginExceptionHandler(Exception e) {
return ResultUtil.error(401, "请重新登录");
}
@ExceptionHandler(NotPermissionException.class)
public ResultVO notPermissionExceptionHandler(NotPermissionException e) {
return ResultUtil.error(512, "您没有该功能使用权限!");
}
}