import org.springframework.core.annotation.Order;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.PrintWriter;

    @Slf4j
    @Order(Integer.MIN_VALUE)
    @Component
    public class ExceptionHandler implements HandlerExceptionResolver {

    1. @Override<br /> public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {<br /> ModelAndView mv = new ModelAndView();<br /> ResultVO result;<br /> try {<br /> response.setContentType(MediaType._APPLICATION_JSON_UTF8_VALUE_);<br /> PrintWriter writer = response.getWriter();<br /> if (ex instanceof BusinessException) {<br /> log.error("BusinessException:{},requestId:{}", ex.getMessage(), LocalContext.getArgument(CommonConstant.REQUEST_ID,String.class),ex);<br /> result = ((BusinessException) ex).getResult();<br /> writer.write(JSON.toJSONString(result));<br /> } else if (ex instanceof ParameterErrorException) {<br /> log.error("ParameterErrorException:{},requestId:{}", ex.getMessage(), LocalContext.getArgument(CommonConstant.REQUEST_ID,String.class),ex);<br /> result = ((ParameterErrorException) ex).getResult();<br /> writer.write(JSON.toJSONString(result));<br /> } else {<br /> log.error("System error,requestId:{}",LocalContext.getArgument(CommonConstant.REQUEST_ID,String.class), ex);<br /> writer.write(JSON.toJSONString(ResultVO.error(CodeEnum.SYSTEM_EXCEPTION)));<br /> }<br /> writer.flush();<br /> writer.close();<br /> } catch (Exception e) {<br /> log.error("error", e);<br /> }<br /> return mv;<br /> }<br />}