1、创建maven工程
2、添加项目依赖
添加父工程
<!-- Import dependency management from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
添加项目依赖
<!--启动器构建web,包括RESTful,使用Spring MVC的应用程序。使用Tomcat作为默认嵌入式容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok:用来消除Java类中的大量样板代码,产生干净,简洁且易于维护的Java类 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
3、添加静态资源
在resources目录下创建static文件夹,将需要使用的静态资源添加到其中
4、添加拦截器
package com.wedu.springmvc.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j //自动生成日志类,等同于private static final Logger log = LoggerFactory.getLogger(MyInterceptor.class);
public class MyInterceptor implements HandlerInterceptor {
//private static final Logger log = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.debug("preHandle method is running");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
log.debug("postHandle method is running");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
log.debug("afterCompletion method is running");
}
}
5、重写mvc配置类,让其实现WebMvcConfigurer接口
package com.wedu.springmvc.config;
import com.wedu.springmvc.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
/**
* 重写接口中的addInterceptors方法,添加自定义拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 通过registry来注册拦截器,通过addPathPatterns来添加拦截路径
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
}
6、编写配置文件application.yml
# WEB PROPERTIES
server:
port: 8081
servlet:
path: /
# LOGGING
logging:
level:
com.wedu.springmvc: debug
6、编写controller
package com.wedu.springmvc.web;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class MvcController {
@RequestMapping("hello")
public String hello() {
log.debug("hello method is running");
return "hello,spring boot !";
}
}
7、创建启动类
package com.wedu.springmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringmvcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringmvcApplication.class,args);
}
}
8、启动测试
控制台输出