根据@ControllerAdvice注解实现ResponseBodyAdvice接口来完成此功能
package com.vshare.decision.config;
import com.vshare.decision.api.Result;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.net.URI;
@ControllerAdvice
public class ResponseBody implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object o,
MethodParameter methodParameter,
MediaType mediaType,
Class aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
URI uri = serverHttpRequest.getURI();
String path = uri.toString();
if (!path.contains("swagger") && !path.contains("v2")) {
Result result = new Result();
if (o == null) {
return result;
} else if (o instanceof Result) {
result = (Result) o;
} else {
result.setResult(o);
}
return result;
}
return o;
}
}