|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.conf; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; | 
|---|
|  |  |  | import org.jetbrains.annotations.NotNull; | 
|---|
|  |  |  | import org.springframework.boot.autoconfigure.http.HttpMessageConverters; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.core.MethodParameter; | 
|---|
|  |  |  | import org.springframework.http.MediaType; | 
|---|
|  |  |  | import org.springframework.http.converter.HttpMessageConverter; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 全局统一返回结果 | 
|---|
|  |  |  | * @author lin | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RestControllerAdvice | 
|---|
|  |  |  | public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | 
|---|
|  |  |  | public boolean supports(@NotNull MethodParameter returnType, @NotNull Class<? extends HttpMessageConverter<?>> converterType) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { | 
|---|
|  |  |  | public Object beforeBodyWrite(Object body, @NotNull MethodParameter returnType, @NotNull MediaType selectedContentType, @NotNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response) { | 
|---|
|  |  |  | // 排除api文档的接口,这个接口不需要统一 | 
|---|
|  |  |  | String[] excludePath = {"/v3/api-docs","/api/v1"}; | 
|---|
|  |  |  | String[] excludePath = {"/v3/api-docs","/api/v1","/index/hook"}; | 
|---|
|  |  |  | for (String path : excludePath) { | 
|---|
|  |  |  | if (request.getURI().getPath().startsWith(path)) { | 
|---|
|  |  |  | return body; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (body instanceof String) { | 
|---|
|  |  |  | return JSON.toJSON(WVPResult.success(body)); | 
|---|
|  |  |  | return JSON.toJSONString(WVPResult.success(body)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return WVPResult.success(body); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 防止返回string时出错 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public HttpMessageConverters fast() { | 
|---|
|  |  |  | return new HttpMessageConverters(new FastJsonHttpMessageConverter()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|