StopWatch #stop(),抛出NPE

时间:2016-10-12 06:41:57

标签: spring-mvc

在使用interceptor SpringMVC工作时我得到了NPE。我使用的是log4j.properties,以下是interceptor

的代码
 ThreadLocal<StopWatch> stopWatchLocal = new ThreadLocal<>();
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
            Exception exception) throws Exception {
        StopWatch stopWatch = stopWatchLocal.get();
        stopWatch.stop();//Line 24
        logger.info("Total time taken for processing: " + stopWatch.getTotalTimeMillis() + " ms");
        stopWatchLocal.set(null);


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
            ModelAndView modelAndView) throws Exception {
        logger.info("Request processing ended on " + getCurrentTime());

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        StopWatch stopWatch = new StopWatch(object.toString());
        stopWatch.start(object.toString());
        stopWatchLocal.set(stopWatch);

        logger.info("Accessing URL path: " + getURLPath(request));
        logger.info("Request processing started on: " + getCurrentTime());
        return true;
    }

    private String getCurrentTime() {
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy 'at' hh:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        return formatter.format(calendar.getTime());
    }

    private String getURLPath(HttpServletRequest request) {
        String currentPath = request.getRequestURI();
        String queryString = request.getQueryString();
        queryString = queryString == null ? "" : "?" + queryString;
        return currentPath + queryString;
    }

这是log

Ĵ

ava.lang.NullPointerException
    at com.webstore.interceptor.PerformanceMontiorInterceptor.afterCompletion(PerformanceMontiorInterceptor.java:24)
    at org.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion(HandlerExecutionChain.java:167)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1023)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
dispatcherServlet

中的

拦截器

<mvc:interceptors>
        <bean class="com.package.InterceptorClass"></bean>
    </mvc:interceptors>

我做错了吗? 请帮助,我的配置是否被破坏,因为我无法得到答案为什么我得到NPE

0 个答案:

没有答案