解决spring boot网关gateway导致的坑,无法下载文件问题

话不多说,直接上图

接口返回内容,浏览器显示PDF文档。但是输入接口地址以后一直提示这个

核对接口路径也是正确的,并且没有报错提示,后面发现是网关没有配置放行路径,于是进行了补充

所以 以后对于前端请求统一由网关进行配置处理的,一定要对于静态资源合理配置,或者对于放行接口要统一补充进来(最后统一一下下接口前缀名称,这样就只需要写一个了)

下面我再说一个坑:

spring cloud gateway启动报错:org.springframework.cloud.gateway.config.GatewayAutoConfiguration

springcloud 启动一直报错 详细错误信息

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.

解决办法

将pom文件中的spring-boot-starter-web 移除

详细分析

点进去 GatewayAutoConfiguration 这个类

@Bean
public ModifyRequestBodyGatewayFilterFactory modifyRequestBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer) {
    return new ModifyRequestBodyGatewayFilterFactory(codecConfigurer.getReaders());
}

@Bean
public DedupeResponseHeaderGatewayFilterFactory dedupeResponseHeaderGatewayFilterFactory() {
    return new DedupeResponseHeaderGatewayFilterFactory();
}

@Bean
public ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer, Set<MessageBodyDecoder> bodyDecoders, Set<MessageBodyEncoder> bodyEncoders) {
    return new ModifyResponseBodyGatewayFilterFactory(codecConfigurer.getReaders(), bodyDecoders, bodyEncoders);
}

确实需要这个bean

spring cloud gateway server项目是一个spring boot项目,在启动的时候会去加载它的配置,其中有一个叫做GatewayClassPathWarningAutoConfiguration的配置类 代码入下

@Configuration(
    proxyBeanMethods = false
)
//在 GatewayAutoConfiguration 执行执行
@AutoConfigureBefore({GatewayAutoConfiguration.class})
public class GatewayClassPathWarningAutoConfiguration {
    private static final Log log = LogFactory.getLog(GatewayClassPathWarningAutoConfiguration.class);
    private static final String BORDER = "\n\n**********************************************************\n\n";
    public GatewayClassPathWarningAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnMissingClass({"org.springframework.web.reactive.DispatcherHandler"})
    protected static class WebfluxMissingFromClasspathConfiguration {
        public WebfluxMissingFromClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring Webflux is missing from the classpath, which is required for Spring Cloud Gateway at this time. Please add spring-boot-starter-webflux dependency.\n\n**********************************************************\n\n");
        }
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass(
        name = {"org.springframework.web.servlet.DispatcherServlet"}
    )
    protected static class SpringMvcFoundOnClasspathConfiguration {
        public SpringMvcFoundOnClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.\n\n**********************************************************\n\n");
        }
    }
}

我们pom中依赖了spring-boot-starter-web 也就是存在

org.springframework.web.servlet.DispatcherServlet

此时这个 与Spring Cloud Gateway不兼容

spring cloud gateway是基于webflux的,如果需要web支持的话需要导入spring-boot-starter-webflux取代spring-boot-start-web。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 在 Spring Boot 项目中实现文件下载功能

    (一)需求 在您的 springboot 项目中,可能会存在让用户下载文档的需求,比如让用户下载 readme 文档来更好地了解该项目的概况或使用方法. 所以,您需要为用户提供可以下载文件的 API ,将用户希望获取的文件作为下载资源返回给前端. (二)代码 maven 依赖 请您创建好一个 springboot 项目,一定要引入 web 依赖: <dependency> <groupId>org.springframework.boot</groupId> <a

  • 详解SpringCloudGateway内存泄漏问题

    SpringCloudGateway内存泄漏问题 项目完善差不多,在进入压力测试阶段期间,发现了gateway有内存泄漏问题,问题发现的起因是,当时启动一台gateway,一台对应的下游应用服务,在压力测试期间,发现特别不稳定,并发量时高时低,而且会有施压机卡住的现象,然后找到容器对应的宿主机,并使用container stats命令观察内存,经过观察发现,压力测试时内存会暴涨,并由于超过限制最大内存导致容器挂掉(这里由于用的swarm所以会自动选择节点重启)最终发现由于之前测试服务器配置低,所

  • 解决spring cloud gateway 获取body内容并修改的问题

    之前写过一篇文章,如何获取body的内容. Spring Cloud Gateway获取body内容,不影响GET请求 确实能够获取所有body的内容了,不过今天终端同学调试接口的时候和我说,遇到了400的问题,报错是这样的HTTP method names must be tokens,搜了一下,都是说https引起的.可我的项目还没用https,排除了. 想到是不是因为修改了body内容导致的问题,试着不修改body的内容,直接传给微服务,果然没有报错了. 问题找到,那就好办了,肯定是我新构

  • 解决SpringBoot打成jar运行后无法读取resources里的文件问题

    开发一个word替换功能时,因替换其中的内容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources 下 在开发环境中通过下面方法能读取word_replace_tpl.docx文件,但是打成jar包在 linux下运行后无法找到文件了 File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/office_template/xxx.docx&q

  • springboot中Excel文件下载踩坑大全

    目录 项目场景:Spring boot文件下载 问题一:下载的文件名称出现中文乱码的问题 问题二:在swagger中测试下载接口,点击下载的文件,发现文件名是乱码的问题 问题四:开发环境下载成功,打成jar包发布到服务器上部署就出现下载失败问题 完整代码 项目场景:Spring boot文件下载 调用接口下载spring boot工程的resources目录下的excel模板文件,非常常见的一个文件下载功能,但是却容易遇到很多坑,下面总结记录下. 问题一:下载的文件名称出现中文乱码的问题 解决方

  • 解决spring boot网关gateway导致的坑,无法下载文件问题

    话不多说,直接上图 接口返回内容,浏览器显示PDF文档.但是输入接口地址以后一直提示这个 核对接口路径也是正确的,并且没有报错提示,后面发现是网关没有配置放行路径,于是进行了补充 所以 以后对于前端请求统一由网关进行配置处理的,一定要对于静态资源合理配置,或者对于放行接口要统一补充进来(最后统一一下下接口前缀名称,这样就只需要写一个了) 下面我再说一个坑: spring cloud gateway启动报错:org.springframework.cloud.gateway.config.Gate

  • 解决spring boot 配置文件后缀的一个坑

    目录 spring boot 配置文件后缀的一个坑 spring boot配置文件支持 properties和yml 从新创建一个demo试试 spring boot 配置文件后缀导致启动失败 错误 错误原因 解决方案 spring boot 配置文件后缀的一个坑 Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot d

  • 解决Spring boot 整合Junit遇到的坑

    目录 这是我在使用springboot整合Junit的时候遇到的坑 1.在pom.xml中添加junit环境的依赖 2.在src/test/java下建立测试类 3.自己编写的启动类 SpringBoot 整合Junit测试注入Bean失败 问题描述 下面是我的测试类 解决过程 以下是我的启动类 总结 这是我在使用springboot整合Junit的时候遇到的坑 1.在pom.xml中添加junit环境的依赖 <dependency> <groupId>org.springfram

  • 解决spring boot 1.5.4 配置多数据源的问题

    spring boot 已经支持多数据源配置了,无需网上好多那些编写什么类的,特别麻烦,看看如下解决方案,官方的,放心! 1.首先定义数据源配置 #=====================multiple database config============================ #ds1 first.datasource.url=jdbc:mysql://localhost/test?characterEncoding=utf8&useSSL=true first.datasou

  • 解决Spring Boot 在localhost域奇怪的404问题(Mac book pro)

    在mac系统中,明明url是对的,浏览器也可以打开,一个简单的代码调用就是404,你有没有遇到过? 情景再现 普通的一个controller,返回一个常量. @GetMapping("/project_metadata/spring-boot") public String getMetadata(){ return "{\"data\":1234}";//这个不重要 } 调用接口的方式: content = new JSONObject(res

  • Spring Boot连接超时导致502错误的实战案例

    1.问题描述 内部系统之间通过Nginx来实现路由转发. 但最近发现有一个系统,经常报502错误,每天达到上百次,完全无法忍受. 2. 原因排查 于是进行排查, 发现配置人员把连接超时时间(server.tomcat.connection-timeout)的单位,理解为秒,实际上是毫秒. SpringBoot的部分配置如下: # Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 c

  • 解决Spring Boot 多模块注入访问不到jar包中的Bean问题

    情景描述 一个聚合项目spring-security-tutorial,其中包括4个module,pom如下所示: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://mav

  • 解决Spring boot整合mybatis,xml资源文件放置及路径配置问题

    网上各种解决方案,我试了好久,整合了几篇文章才凑出来,在这里分享一下,实在不想网友们在这里面绕圈子,毕竟,写代码的时间是愉快的,解决bug也是愉快的,但也是一直在bug里面绕圈子就不爽了. 亲自试验: 1) 我的mapper和xml是这样子放置的 2) 在.xml中namespace是这样的: 3) application.properties中mybatis.mapper-locations得这么配置到xml 4) 最后呢,你只要在pom.xml中build下这样配置 5) 按照我这种位置防止

  • 解决Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题

    LocalDate . LocalTime . LocalDateTime 是Java 8开始提供的时间日期API,主要用来优化Java 8以前对于时间日期的处理操作.然而,我们在使用Spring Boot或使用Spring Cloud Feign的时候,往往会发现使用请求参数或返回结果中有 LocalDate . LocalTime . LocalDateTime 的时候会发生各种问题.本文我们就来说说这种情况下出现的问题,以及如何解决. 问题现象 先来看看症状.比如下面的例子: @Sprin

  • 解决Spring Boot 正常启动后访问Controller提示404问题

    问题描述 今天重新在搭建Spring Boot项目的时候遇到访问Controller报404错误,之前在搭建的时候没怎么注意这块.新创建项目成功后,作为项目启动类的Application在com.blog.start包下面,然后我写了一个Controller,然后包的路径是com.blog.ty.controller用的@RestController 注解去配置的controller,然后路径也搭好了,但是浏览器一直报404.最后找到原因是Spring Boot只会扫描启动类当前包和以下的包 ,

随机推荐