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

时间:2021-12-30 15:32:08

话不多说,直接上图

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

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

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

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

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

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

下面我再说一个坑:

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 = "

**********************************************************

";
    public GatewayClassPathWarningAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnMissingClass({"org.springframework.web.reactive.DispatcherHandler"})
    protected static class WebfluxMissingFromClasspathConfiguration {
        public WebfluxMissingFromClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("

**********************************************************

Spring Webflux is missing from the classpath, which is required for Spring Cloud Gateway at this time. Please add spring-boot-starter-webflux dependency.

**********************************************************

");
        }
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass(
        name = {"org.springframework.web.servlet.DispatcherServlet"}
    )
    protected static class SpringMvcFoundOnClasspathConfiguration {
        public SpringMvcFoundOnClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************

");
        }
    }
}

我们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。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_38239565/article/details/101193685