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 小时前
【python】pyspark.errors.exceptions.base.PySparkRuntimeError [JAVA_GATEWAY_EXITED] Java gateway proce
java·python·gateway
YJlio1 小时前
OpenClaw v2026.5.26-beta.1 / beta.2 预发布解读:Gateway 加速、transcript 路径统一、多通道修复、语音增强与安装更新链路加固
人工智能·windows·python·ui·缓存·gateway·outlook
普通网友1 小时前
AWS VPC Transit Gateway 部署:实现多 VPC(开发 / 测试 / 生产)间流量集中管控
云计算·gateway·aws
2601_9611940210 小时前
2026初级会计实务公式总结大全|计算题公式手册PDF
java·spring·eclipse·pdf·tomcat·hibernate
phltxy13 小时前
Spring AI 从提示词到多模态
java·人工智能·spring
starsky7623817 小时前
基于 Spring AI 构建具备记忆与情绪的多角色 Agent 系统
人工智能·spring·架构
王五周八17 小时前
玩转 Spring AI Agent:基于 SpringBoot 集成 AI 工具与 Skills 能力实践
java·spring
gCode Teacher 格码致知19 小时前
AspNet Mvc教学:Route路由切换-由Deepseek产生
mvc
云烟成雨TD20 小时前
Spring AI Alibaba 1.x 系列【67】ReactAgent SSE 流式输出
java·人工智能·spring