关于springboot拦截器WebMvcConfigurerAdapter过时处理

 WebMvcConfigurerAdapter使用时提示已过时,看网上建议使用WebMvcConfigurationSupport,但使用这个会是springboot的自动配置有问题:@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)。所以建议实现 implements WebMvcConfigurer。

代码附上:

拦截器:

package com.abc.interceptor;
        /*
         *  author  :  ajing
         *  date    :  2018/4/4 15:53
         * */

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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



public class WebIntercrptor  extends HandlerInterceptorAdapter{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime=System.currentTimeMillis();
        request.setAttribute("startTime",startTime);
        System.out.println("前拦截器处理完毕");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (long)  request.getAttribute("startTime");
        long endTime=System.currentTimeMillis();
        System.out.println("后拦截器处理完毕,本次处理时间为:"+(endTime-startTime));
        request.setAttribute("handleTime",endTime-startTime);
    }
}

拦截器配置:

package com.abc.interceptor;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/*
 *  author  :  ajing
 *  date    :  2018/4/9 14:55
 * */
@Configuration
public class webInterceptorAdapter implements WebMvcConfigurer {
    @Bean
    public  WebIntercrptor webIntercrptor(){
        return new WebIntercrptor();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/error").setViewName("404.html");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webIntercrptor()).addPathPatterns("/**");
    }
    //注入视图处理器bean
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver resourceViewResolver=new InternalResourceViewResolver();
        resourceViewResolver.setPrefix("/templates/");
        resourceViewResolver.setSuffix(".html");
        resourceViewResolver.setViewClass(JstlView.class);
        return resourceViewResolver;
    }
    


    //添加文件是暴露路径和文件的实际访问路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/view").addResourceLocations("/");
    }
}

controller:

@RequestMapping(value = "getPlanNoteVersion", method = {RequestMethod.GET,RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public PlanNote getPlanNoteVersion(){
        PlanNote planNote=new PlanNote();
       // planNote.setVersion(area);
        planNote.setOwner(propertiesBean.getOwner());
        logger.info("【获取笔记版本和开发者信息】");
        return planNote;
    }

启动类:

package com.abc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication   //开启Spring Boot的自动配置
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值