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";
    }
}
相关推荐
qfljg1 小时前
OpenRewrite 实战指南
java
Sylvia33.1 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议
2601_962071576 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
zww89491117 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·eclipse
zww894911110 小时前
匿名树洞系统开发实战:从需求分析到部署指南
java·eclipse
黑马程序员毕设11 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
木井巳11 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法
白山编程大哥11 小时前
Java 集合算法:从排序、查找到底层原理的实战指南
java·python·算法
devpotato12 小时前
缓存与数据库更新顺序不一致问题
java·数据库·redis
xcl092512 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·spring boot·需求分析