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);
}
}