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.

相关推荐
fundoit1 小时前
为什么需要 Access Token 和 ID Token 两个令牌
java·spring·架构·github·oauth2
鱼宵2 小时前
Spring AI 流式输出:Flux + SSE 打字机,回答不再干等三秒
java·人工智能·spring·sse·springai·流式输出
小鹿的周先生3 小时前
第19章-Agent
java·spring·ai
步行cgn4 小时前
MySQL 报错:Access denied for user ‘root‘@‘localhost‘ 详解
java·数据库·spring
鱼宵5 小时前
Spring AI 生产化改造:记忆落 Redis、向量落 ES,重启再也不丢
人工智能·redis·spring·elasticsearch·springai
Wang's Blog5 小时前
Java框架 SpringCloud 快速入门: Nacos 配置热更新
java·spring·spring cloud
天空鸟_时光不老6 小时前
06-给AI流程加一道人工闸门
java·人工智能·spring boot·后端·spring·spring cloud·架构
阿俊-全栈开发7 小时前
LikeShop单商户Java商城如何从容承接高并发流量?
java·开发语言·spring boot·spring·系统架构
天空鸟_时光不老7 小时前
01-我不转Python把AI塞进Java里
java·人工智能·spring boot·后端·spring·spring cloud·架构
桃西西呀7 小时前
Spring AI Alibaba 之四:拆开 ReactAgent 的图,看 ReAct 循环怎么拼出来
人工智能·spring·llm