Springboot整合之Servlet、Filter、Interceptor

本文详细介绍了如何在Springboot应用中整合Servlet、Filter和Interceptor。从springboot入门开始,逐步演示了通过注解扫描和方法注册这两种方式完成Servlet和Filter组件的注册,并展示了对应的HTTP请求结果。此外,还提到了SpringBoot整合Interceptor的步骤,包括POM配置、自定义拦截器的实现以及拦截器的添加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot整合之Servlet、Filter、Interceptor

目录:1、springboot入门 2、springboot整合servlet 3、springboot整合filter

一、springboot入门

•  Spring Boot设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。 
•  嵌入的 Tomcat,无需部署 WAR 文件 
•  Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。



POM.xml

    <!--springboot项目的父类,所有springboot项目都必须继承于它-->
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

	<!--springboot启动器-->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
	</dependency>



springBoot启动器其实就是一些jar包的集合。SprigBoot一共提供44启动器。
    1 spring-boot-starter-web
    支持全栈式的web开发,包括了romcat和springMVC等jar
    2 spring-boot-starter-jdbc
    支持spring以jdbc方式操作数据库的jar包的集合
    3 spring-boot-starter-redis
    支持redis键值存储的数据库操作



Controller

package com.lee.controller;

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

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorld {
   

    @RequestMapping("/hello")
    public Map<String,Object> hello(){
   
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("msg","hello world");
        return resultMap;
    }

}



启动类

package com.lee;

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

@SpringBootApplication
public class SpringbootApplication {
   

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

}



@SpringBootApplication包含:
	@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan、@Configuration等注解,它是一个配置类,扫描了当前包和当前包下所有子包下的所有文件



结果:http://localhost:8080/hello

{"msg":"hello world"}



二、Springboot整合Servlet

两种方式完成组件的注册:

1、通过注解扫描完成组件的注册

FirstServlet

package com.lee.servlet;

import javax.servlet.ServletException;
import javax.servlet
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值