@ControllerAdvice(basePackages = "cn.sqinet.app.manage.web.controller") @Slf4j public class LogResponseAdvice implements ResponseBodyAdvice<Object> { @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { log.warn("返回值为:{}", JSON.toJSONString(body)); BaseResult baseResult = new BaseResult(); JSONObject jo = null; if (body instanceof BaseResult) { baseResult = (BaseResult) body; jo = JSONObject.parseObject(JSONObject.toJSONString(body)); if (jo != null && baseResult != null && StringUtils.isBlank(baseResult.getCode())) { if (baseResult.getSuccess()) { jo.put("code", "0"); } else { jo.put("code", "1"); } return jo; } } return body; } }
@ResponseBody @ExceptionHandler(ResultException.class) public String handleException(ResultException ex) { String meMessage = ex.getErrorMsg(); int errorCode = ex.getErrorCode(); BaseResult bean = new BaseResult(String.valueOf(errorCode),meMessage, false); String jsonString = JSONObject.toJSONString(bean); LOGGER.error(MessageFormat.format("response", jsonString)); return jsonString; }