package net.hehe.web;

import static org.springframework.util.Assert.notNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author: TaoLei
 * date: 2015/11/21.
 * description:
 */
public class ExceptionHandler extends AbstractHandlerExceptionResolver implements InitializingBean {
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    //数据库相关异常
    private String foreignErr = "a foreign key constraint fails";
    private String foreignMsg = "该资源被其它业务占用,无法删除";
    private MappingToJsonView viewName;

    public void setViewName(MappingToJsonView viewName) {
        this.viewName = viewName;
    }

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
                                              Object handler, Exception e) {
        log.error("Catch Exception: ", e);
        ModelAndView mav = new ModelAndView(viewName);
        mav.addObject(ResponseStatus.SUCCESS, false);
        String msg = e.getLocalizedMessage();
        if (msg.indexOf(foreignErr) > 0) {
            msg = foreignMsg;
        }
        mav.addObject(ResponseStatus.ERROR, msg);
        return mav;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        checkHandlerConfig();
    }

    protected void checkHandlerConfig() {
        notNull(this.viewName, "Property 'viewName' is required");
    }
}

@@ -555,36 +555,8 @@
 ver 
-implements InitializingBean 
 %7B%0A  
@@ -1449,230 +1449,7 @@
    %7D
-%0A%0A    @Override%0A    public void afterPropertiesSet() throws Exception %7B%0A        checkHandlerConfig();%0A    %7D%0A%0A    protected void checkHandlerConfig() %7B%0A        notNull(this.viewName, %22Property 'viewName' is required%22);%0A    %7D
 %0A%7D%0A

remove forced viewName config