zoukankan      html  css  js  c++  java
  • @ControllerAdvice注解的使用

    package com.vcredit.ddcash.monitor.controller;

    import com.vcredit.ddcash.monitor.model.dto.Response;
    import freemarker.template.utility.StringUtil;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;

    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Locale;
    import java.util.Map;

    /**
    * Created by guanshuli on 2017/1/11.
    */
    @ControllerAdvice
    public class GlobalExceptionHandler {
    public static final String DEFAULT_ERROR_VIEW = "error";
    public static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public Object defaultErrorHandler(Throwable e,
    HttpServletRequest request) {
    Response response=new Response();
    e.printStackTrace();
    response.setStatus("接口异常");
    response.setMsg("网络异常,请稍后重试");
    this.writeLog(request, e);
    return response;
    }

    private void writeLog(HttpServletRequest request, Throwable e) {
    Map<String, String> map = new HashMap<String, String>();
    Enumeration<String> enumeration = request.getHeaderNames();
    while (enumeration.hasMoreElements()) {
    String str = enumeration.nextElement().toString();
    map.put(str, request.getHeader(str));
    }
    map.put("getRequestURI", request.getRequestURI());
    logger.error("请求异常,请求头:{};", map.toString(), e);

    }
    }

  • 相关阅读:
    ASP程序调用验证码
    fcex pf.conf
    高速INTERNET代理服务器解决方案
    《IIS 6的几个经典问答》
    一个符合WEB标准的横向下拉菜单
    FREEBSD+PF 在6.2上的架设
    权限
    FreeBSD+IPFILTER实现整网(N个Vlan)透明代理上网
    pf 带宽控制 例子
    FreeBSD下配置DHCP服务小结
  • 原文地址:https://www.cnblogs.com/muliu/p/6273740.html
Copyright © 2011-2022 走看看