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";
    }
}
相关推荐
Lhappy嘻嘻7 小时前
Java IO|File 文件操作 + 字节流 / 字符流完整笔记 + 递归删除文件实战
java·笔记·php
伊玛目的门徒8 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
懒鸟一枚10 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
我命由我1234512 小时前
执行 Gradle 指令报错,无法将“grep”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
考虑考虑12 小时前
Sentinel安装
java·后端·微服务
凤凰院凶涛QAQ14 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
掘金_答案14 小时前
上线那天,一个 ConcurrentHashMap 差点送走我的 AI 客服——3 天排查 JVM 血泪史
java·后端·架构
猿与禅15 小时前
CosId 分布式 ID 生成器完全教程:从架构原理到生产落地
java·shardingsphere·雪花算法·分布式id·高性能·cosid·号段模式
MindUp15 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
NG47715 小时前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程