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.

相关推荐
阿维的博客日记7 小时前
MultipartFile 是不是表示仅仅是一个分片?
java·后端·spring·multipartfile
一路向北North7 小时前
Spring Security OAuth2.0(23):分布式系统授权-转发明文给微服务
java·spring·微服务
专注_每天进步一点点8 小时前
SLB(绑定弹性公网ip)-gateway-业务pod,gateway上出现reset by peer,业务pod上没有reset by peer
服务器·tcp/ip·gateway
weixin_7275356213 小时前
Spring @Transactional 事务失效:原理层面深度解析
java·spring
没钥匙的锁114 小时前
16-Java反射机制:Spring IOC背后的核心技术
java·开发语言·spring
春卷同学15 小时前
HarmonyOS掌上记账APP开发实践第23篇:弹性动效与手势 — EdgeEffect.Spring 与手势系统实战
spring·华为·harmonyos
大不点wow18 小时前
Spring 中 @Bean、@Component 与 @Configuration 的作用及底层逻辑
java·spring
大不点wow18 小时前
为什么 Spring 更推荐构造器注入,而不是字段 `@Autowired`
java·spring·intellij-idea
卓怡学长1 天前
w255基于springboot仓库管理系统
java·数据库·spring boot·spring·intellij-idea
csdn2015_1 天前
springboot读取配置的方法
java·spring boot·spring