解決spring boot網(wǎng)關(guān)gateway導(dǎo)致的坑,無法下載文件問題
話不多說,直接上圖
接口返回內(nèi)容,瀏覽器顯示PDF文檔。但是輸入接口地址以后一直提示這個(gè)
核對(duì)接口路徑也是正確的,并且沒有報(bào)錯(cuò)提示,后面發(fā)現(xiàn)是網(wǎng)關(guān)沒有配置放行路徑,于是進(jìn)行了補(bǔ)充
所以 以后對(duì)于前端請(qǐng)求統(tǒng)一由網(wǎng)關(guān)進(jìn)行配置處理的,一定要對(duì)于靜態(tài)資源合理配置,或者對(duì)于放行接口要統(tǒng)一補(bǔ)充進(jìn)來(最后統(tǒng)一一下下接口前綴名稱,這樣就只需要寫一個(gè)了)
下面我再說一個(gè)坑:
spring cloud gateway啟動(dòng)報(bào)錯(cuò):org.springframework.cloud.gateway.config.GatewayAutoConfiguration
springcloud 啟動(dòng)一直報(bào)錯(cuò) 詳細(xì)錯(cuò)誤信息
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 移除
詳細(xì)分析
點(diǎn)進(jìn)去 GatewayAutoConfiguration 這個(gè)類
@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); }
確實(shí)需要這個(gè)bean
spring cloud gateway server項(xiàng)目是一個(gè)spring boot項(xiàng)目,在啟動(dòng)的時(shí)候會(huì)去加載它的配置,其中有一個(gè)叫做GatewayClassPathWarningAutoConfiguration的配置類 代碼入下
@Configuration( proxyBeanMethods = false ) //在 GatewayAutoConfiguration 執(zhí)行執(zhí)行 @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
此時(shí)這個(gè) 與Spring Cloud Gateway不兼容
spring cloud gateway是基于webflux的,如果需要web支持的話需要導(dǎo)入spring-boot-starter-webflux取代spring-boot-start-web。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring解決循環(huán)依賴問題的三種方法小結(jié)
在 Spring 中,循環(huán)依賴問題指的是兩個(gè)或多個(gè) bean 之間相互依賴形成的閉環(huán),具體而言,當(dāng) bean A 依賴于 bean B,同時(shí) bean B 也依賴于 bean A,就形成了循環(huán)依賴,本文就給大家介紹了Spring解決循環(huán)依賴問題的三種方法,需要的朋友可以參考下2023-09-09Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查詳解
這篇文章主要給大家介紹了關(guān)于Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Maven導(dǎo)入Junit4后在test中無法引用問題
在使用Maven進(jìn)行項(xiàng)目管理時(shí),可能會(huì)遇到導(dǎo)入依賴和打開項(xiàng)目結(jié)構(gòu)的問題,本文通過實(shí)際經(jīng)驗(yàn),提供了一些解決方法和技巧,希望能幫助遇到相同問題的開發(fā)者,此外,還鼓勵(lì)大家多多支持和分享個(gè)人經(jīng)驗(yàn),以便于共同進(jìn)步2024-10-10maven 在執(zhí)行package,install,deploy時(shí)使用clean與不使用clean的不同之處
有時(shí)候用mvn install后,新改的內(nèi)容不生效,一定要后來使用mvn clean install 才生效,由于之前沒有做記錄,以及記不清是什么情況下才會(huì)出現(xiàn)的問題,于是想看看clean和不clean的區(qū)別,感興趣的朋友跟隨小編一起看看吧2021-08-08Java基礎(chǔ)教程之final關(guān)鍵字淺析
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之final關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06SpringBoot如何接收Post請(qǐng)求Body里面的參數(shù)
這篇文章主要介紹了SpringBoot如何接收Post請(qǐng)求Body里面的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03