2022-08-21 22:22:34 +08:00
|
|
|
|
package com.genersoft.iot.vmp.conf;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
|
|
|
|
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
2022-08-22 16:16:31 +08:00
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-08-21 22:22:34 +08:00
|
|
|
|
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.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全局统一返回结果
|
2022-08-22 16:16:31 +08:00
|
|
|
|
* @author lin
|
2022-08-21 22:22:34 +08:00
|
|
|
|
*/
|
|
|
|
|
@RestControllerAdvice
|
|
|
|
|
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-22 16:16:31 +08:00
|
|
|
|
public boolean supports(@NotNull MethodParameter returnType, @NotNull Class<? extends HttpMessageConverter<?>> converterType) {
|
2022-08-21 22:22:34 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-22 16:16:31 +08:00
|
|
|
|
public Object beforeBodyWrite(Object body, @NotNull MethodParameter returnType, @NotNull MediaType selectedContentType, @NotNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response) {
|
2022-08-21 22:22:34 +08:00
|
|
|
|
// 排除api文档的接口,这个接口不需要统一
|
2022-08-22 16:16:31 +08:00
|
|
|
|
String[] excludePath = {"/v3/api-docs","/api/v1","/index/hook"};
|
2022-08-21 22:22:34 +08:00
|
|
|
|
for (String path : excludePath) {
|
|
|
|
|
if (request.getURI().getPath().startsWith(path)) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body instanceof WVPResult) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body instanceof ErrorCode) {
|
|
|
|
|
ErrorCode errorCode = (ErrorCode) body;
|
|
|
|
|
return new WVPResult<>(errorCode.getCode(), errorCode.getMsg(), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body instanceof String) {
|
2022-08-22 16:16:31 +08:00
|
|
|
|
return JSON.toJSONString(WVPResult.success(body));
|
2022-08-21 22:22:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WVPResult.success(body);
|
|
|
|
|
}
|
|
|
|
|
}
|