springboot 整合 springMvc(包含springmvc的拦截器的使用)

文章目录

项目目录

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 所有springboot项目,都必须继承自 spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>

    <groupId>com.atguigu</groupId>
    <artifactId>boot-mvc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!--
            SpringBoot提供了许多预定义的Starter,如:
                spring-boot-starter-web用于构建Web应用程序,
                spring-boot-starter-data-jpa用于使用JPA进行数据库访问,
                spring-boot-starter-security用于安全认证和授权,
                ...等等

            使用Starter非常简单,只需要在项目的构建文件(例如Maven的pom.xml)中添加所需的Starter依赖,
            SpringBoot会自动处理依赖管理和配置。

            springboot提供的全部启动器地址:
                [https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters)

            springboot启动器的命名规范:
                官方提供的:命名为:`spring-boot-starter-*`
                第三方提供:命名为:`*-spring-boot-starter`
        -->

        <!--web开发的场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
    </dependencies>

</project>

Main 程序入口

java 复制代码
package com.atguigu;

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

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

配置文件application.yml

自定义外部资源文件夹的路径

yml 复制代码
# springMvc相关的配置
server:
  port: 80 # 端口号,默认是8080
  servlet:
    context-path: /boot # 设置项目根路径

spring:
  web:
    resources:
      # 这是默认值
      # static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/
      static-locations: classpath:/webapp # 自定义外部资源文件夹的路径
      # 一旦自定义外部资源文件夹路径,就覆盖掉默认路径了
      # 访问外部资源的时候,不要写外部资源文件夹的名字哦。。。

resources / static / login.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录界面</title>
</head>
<body>
    登录界面
</body>
</html>

resources / webapp/ register.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册Title</title>
</head>
<body>
    注册界面
</body>
</html>

拦截器

java 复制代码
package com.atguigu.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("request = " + request + "response = " + response + "handler = " + handler);
        return true;
    }
}

springMvc配置文件 WebMvcConfig .java

  • springboot 项目中,你 springmvc 的功能,就像之前一样用就行了

  • 加上 @Configuration注解

  • 只要在 MainApplication.java类所在包及其子包下,就会被 @SpringBootApplication 注解扫描注册

java 复制代码
package com.atguigu.config;

import com.atguigu.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * springboot项目中,你springmvc的功能就像之前一样用就行了
 *
 * 加上 @Configuration注解
 *
 * 只要在 MainApplication.java类 所在包及其子包下,就会被@SpringBootApplication注解扫描注册
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}

controller

java 复制代码
package com.atguigu.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {

    @GetMapping("show")
    public String show(){
        return "hello - show";
    }
}
相关推荐
小bo波7 小时前
使用Thread子类创建线程 VS 使用Runnable接口创建线程的区别
java·多线程·thread·并发编程·runnable
SamDeepThinking7 小时前
高并发场景下,CompletableFuture与ForkJoinPool该如何取舍?
java·后端·面试
张不才10 小时前
CPU 100% 了怎么办?Java 性能排障的标准化操作
java·后端
shepherd11112 小时前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
plainGeekDev14 小时前
单例模式 → object 声明
android·java·kotlin
用户2986985301415 小时前
Java 实现 Word 文档文本与图片提取的方法
java·后端
SimonKing16 小时前
铁子,IntelliJ IDEA 2026.1.3来了,升不升?
java·后端·程序员
咖啡八杯1 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
用户128526116021 天前
我把祖传Java项目重构后,接口响应从3s砍到了200ms,只改了这几行代码
java