Spring MVC and Spring Gateway 的差异,以及报错处理

首先,报错处理。

复制代码
@Configuration   
public class MvcInterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new UserinfoInterceptor());
    }
}

会报错。这是因为 Spring MVC 和 Spring Gateway 冲突。

网关 webflux

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

Spring MVC

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

或者

解决办法,让 interceptor在仅在 MVC 框架生效。

java 复制代码
@Configuration
@ConditionalOnClass(DispatcherServlet.class)
public class MvcInterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new UserinfoInterceptor());
    }
}

@ConditionalOnClass(DispatcherServlet.class)

仅 MVC 框架有 Servlet Class 。

通过 Google AI 获得。

特性 Spring MVC Spring Gateway
作用 构建Web 应用程序 构建API 网关
目标 用户请求的处理,返回数据和渲染页面 外部请求的过滤、路由、安全控制
核心概念 Model, View, Controller Route, Predicate, Filter
应用场景 传统Web 应用程序 API 接口的管理,微服务架构中的网关

Spring cloud Gateway 底层

是 Netty 框架。

Netty: Home

更多细节,参考 netty 网站。

Netty is an NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

'Quick and easy' doesn't mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.

相关推荐
唐青枫1 天前
Java Spring WebFlux 实战指南:用 Mono、Flux 和 WebClient 写响应式接口
java·spring
咖啡八杯3 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
Flittly4 天前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
dunky4 天前
Spring 的三级缓存与循环依赖
后端·spring
码云数智-园园9 天前
C++20 Modules 模块详解
java·开发语言·spring
咖啡八杯9 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
Flittly9 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
李少兄9 天前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解
java·后端·spring
shushangyun_9 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
ofoxcoding9 天前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai