import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 自定義統(tǒng)一返回值注解
*/
@Retention(RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
public @interface ResponseResult {
}
/**
* 公共的靜態(tài)常量類
*/
public class CommonFinal {
//由于處理統(tǒng)一返回值時打標(biāo)記
public static final String RESPONSE_RESULT_ANN="RESPONSE-RESULT-ANN";
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
//請求攔截器
@Slf4j
@Component
public class ResponseResultInterceptor implements HandlerInterceptor {
// 代碼核心思想,就是獲取此請求鹏溯,是否需要返回值包裝罢维,設(shè)置一個屬性標(biāo)記。
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// log.info("-------------進入攔截器打標(biāo)記------");
if(handler instanceof HandlerMethod){
final HandlerMethod handlerMethod= (HandlerMethod) handler;
final Class<?> clazz=handlerMethod.getBeanType();
final Method method=handlerMethod.getMethod();
//判斷是否在類對象上加了注釋
if(clazz.isAnnotationPresent(ResponseResult.class)){
//設(shè)置此請求返回體丙挽,需要包裝肺孵,往下傳遞,在ResponseBodyAdvice接口進行判斷
request.setAttribute(CommonFinal.RESPONSE_RESULT_ANN,clazz.getAnnotation(ResponseResult.class));
}
//判斷方法上是否有注釋
else if(method.isAnnotationPresent(ResponseResult.class)){
//設(shè)置此請求返回體颜阐,需要包裝平窘,往下傳遞,在ResponseBodyAdvice接口進行判斷
request.setAttribute(CommonFinal.RESPONSE_RESULT_ANN,method.getAnnotation(ResponseResult.class));
}
}
return true;
}
}
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.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.servlet.http.HttpServletRequest;
/**
* 上面代碼就是判斷是否需要返回值包裝凳怨,如果需要就直接包裝瑰艘。這里我們只處理了正常成功的包裝,如果方法體報異常怎么辦猿棉?處理異常也比較簡單磅叛,只要判斷body是否為異常類。
*/
@Slf4j
@ControllerAdvice
public class ResponseResultHandler implements ResponseBodyAdvice<Object> {
//是否請求萨赁,包含了包裝弊琴,注解,標(biāo)記杖爽,沒有就直接返回敲董,不要重寫返回體
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
// log.info("判斷是否打有標(biāo)記 --------------------------");
ServletRequestAttributes sra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=sra.getRequest();
//判斷請求是否包含標(biāo)記
ResponseResult responseResult= (ResponseResult) request.getAttribute(CommonFinal.RESPONSE_RESULT_ANN);
return responseResult==null?false:true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
// log.info("進入返回體 重寫格式 處理中 --------------------------");
return Result.success(body);
}
}