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.

相关推荐
卓怡学长6 小时前
w176基于SpringBoot的医院管理系统
java·spring boot·spring·maven·intellij-idea
大模型码小白6 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
吴声子夜歌7 小时前
Java面试——Spring原理及应用(一)
java·spring·面试
weixin_399380699 小时前
TongWeb7.0.4.9M11一键安装脚本(by sy)
java·spring
叫我Paul就好12 小时前
Spring 为何没有在 Java之外的地方存在?
java·后端·spring
爱码猿14 小时前
SpringAop通过el表达式动态获取方法入参的值
spring boot·spring·aop
东小西16 小时前
【SAA实战】第 2 篇:模型与消息——ReactAgent 怎么挑模型、怎么传消息
java·后端·spring
就叫飞六吧18 小时前
Spring 动态注册与移除 Bean 科普
java·后端·spring
晓晓_za89866818 小时前
多租户 GEO 优化系统源码架构:权限隔离与数据分离实现
java·tcp/ip·spring·缓存·微服务·架构
choumou_M19 小时前
MySQL_1:数据库悲观锁
后端·spring·spring cloud