SpringBoot access static resources and configuration springMVC interceptors

Now, our project is a project jar, then there is no webapp, where our static resources that put it?

The default path for the static resources:

  - classpath:/META-INF/resources/
  - classpath:/resources/
  - classpath:/static/
  - classpath:/public/

  As long as static resources in any of these directories, SpringMVC will help us deal with.

Add interceptor: interceptor is what we often need to use, in SpringBoot in how to configure it?

  To implement a custom section SpringMvc configured using `WebMvcConfigurer` and add` @ Configuration` comment.

  First, we define an interceptor:

@Component
public class MyInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    System.out.println("preHandle method is running!");
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle method is running!");
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    System.out.println("afterCompletion method is running!");
  }
}

  Then define the configuration class, registered interceptors:

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
  @Autowired
  private HandlerInterceptor myInterceptor;

  /**
  * Override addInterceptors interface method, add a custom interceptor
  * @param registry
  */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(myInterceptor).addPathPatterns("/**");
  }
}

  Next, run and view the log:

    preHandle method is running!
    postHandle method is running!
    afterCompletion method is running!

    These logs will find only print information, log information springMVC are not, because the log level springMVC record is debug, springboot default display info above, we need to be configured.

    SpringBoot by `logging.level. * = Debug` to configure the log level, to fill the package name *

      Provided org.springframework packet logging level to Debug
      logging.level.org.springframework = Debug

 

Guess you like

Origin www.cnblogs.com/roadlandscape/p/12394966.html