ExceptionInterceptor.java 717 B

123456789101112131415161718192021222324
  1. package common.interceptor;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jfinal.aop.Interceptor;
  4. import com.jfinal.aop.Invocation;
  5. import com.jfinal.core.Controller;
  6. import com.jfinal.json.FastJson;
  7. import common.utils.http.MyRet;
  8. public class ExceptionInterceptor implements Interceptor {
  9. @Override
  10. public void intercept(Invocation inv) {
  11. try {
  12. inv.invoke();
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. Controller c = inv.getController();
  16. String requestJsonStr = (String) c.getRequest().getAttribute("data");
  17. JSONObject requestJson = FastJson.getJson().parse(requestJsonStr, JSONObject.class);
  18. c.renderJson(MyRet.fail("Exception: " + e.getMessage()).setData(requestJson));
  19. }
  20. }
  21. }