@ControllerAdvice
public class ResponseDataHandler implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter mp, Class type) {
return true;
}
@Override
public Object beforeBodyWrite(Object t, MethodParameter mp, MediaType mt, Class type, ServerHttpRequest shr, ServerHttpResponse shr1) {
if (t instanceof TableResult || t instanceof ResultData) {
return t;
} else {
ResultData data = new ResultData();
data.setData(t);
return data;
}
}
public boolean isAjax(HttpServletRequest request) {
return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"))
|| request.getHeader("Accept") != null
&& request.getHeader("Accept").contains("application/json");
}
}
拦截ajax请求应答,对数据进行统一封装
最新推荐文章于 2023-04-29 16:20:05 发布