2 回答
TA贡献1995条经验 获得超2个赞
最后,是通过增加了一个HandlerInterceptor,这个方法可行,但是处理异常的地方就变成了两处,另一个是controller层异常的统一处理,总觉得不是太好的方案。
@Component
public class PreControllerExceptionIntercep implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(PreControllerExceptionIntercep.class);
@Override
public void afterCompletion(HttpServletRequest request , HttpServletResponse response,Object handler,Exception ex) throws Exception{
if(ex instanceof UnauthenticatedException){
log.info("the access isn't valid");
PackVo packVo = new PackVo();
packVo.setSuccess(false);
packVo.addMsg(BizExceptionCode.NO_PERMISSION_EXCEPTION,"");
String jsonPack = JSON.toJSONString(packVo);
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
PrintWriter out = response.getWriter();
out.print(jsonPack);
out.flush();
out.close();
}
}
- 2 回答
- 0 关注
- 1872 浏览
添加回答
举报