package com.changhong.epc.zuul.filter.advice; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.zuul.filter.Des1; import com.iemsoft.framework.cloud.core.base.ResMsg; import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException; import com.iemsoft.framework.cloud.core.tools.MsgTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; 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; @RestControllerAdvice(basePackages="com.changhong.epc") public class EpcResponseBodyAdvice implements ResponseBodyAdvice, BaseConst { @Override public boolean supports(MethodParameter returnType, Class> converterType) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { if(body instanceof ResMsg){ return body; } ResMsg bm = null; if(body instanceof Exception){ bm = getErrEntity((Exception) body); }else{ bm = getSuccEntity(body); } return bm; } /** * 处理错误消息 * @Title: getErrEntity * @param @param e * @param @return 设定文件 * @return BasicMsg 返回类型 * @throws */ private ResMsg getErrEntity(Exception e){ ResMsg bm = new ResMsg<>(); if(e instanceof IEMRuntimeException){ IEMRuntimeException be = (IEMRuntimeException) e; bm.setRspCode(be.getCode()); bm.setRspMsg(be.getMessage()); } /* * 其它 */ else{ //未知异常 bm.setRspCode(C0018); bm.setRspMsg(e.getMessage()); } return bm; } /** * 处理正确消息 * @Title: getSuccEntity * @param @param data * @param @return 设定文件 * @return BasicMsg 返回类型 * @throws */ public static ResMsg getSuccEntity(Object data){ ResMsg bm = new ResMsg<>(); data = getData(data); if(ObjectUtil.notEmpty(data)){ bm.setData(data); } bm.setRspCode(C0000); bm.setRspMsg(MsgTool.get(C0000)); return bm; } /** * 获得出参 * @Title: getData * @param @param data * @param @return 设定文件 * @return Object 返回类型 * @throws */ private static Object getData(Object data){ // if(data instanceof Page){ // return new PageEntity((Page) data); // } return data; } }