我使用 Spring Cloud Zuul 代理创建了一个网关应用程序。
网关应用程序工作正常,但它没有加载静态文件,如 img、css 和 js 文件。
我在我的中添加了以下设置应用程序属性我的网关应用程序的文件
zuul.routes.demo.url=http://本地主机:8091 http://localhost:8091
where demo的值是spring.application.name我的 Spring Boot 应用程序的属性。
如果我运行我的 spring boot 应用程序,那么所有 css 或 js 或像 img 这样的静态文件加载得很好。
但是当我使用 Zuul 访问同一个应用程序时,它不起作用。
目前我已将静态文件(例如 img)放置在src/main/resources/META-INF/resources/WEB-INF/resources/img/
以下是我尝试过的事情/我的观察。
1)尝试使用剥离前缀: false网关应用程序中的属性。
2)我还尝试将img文件放在src/main/webapp/static/和src/main/resources/static/下
3)我的应用程序和网关应用程序的img链接如下
实际应用:http://localhost:8091/img/test.svg http://localhost:8091/img/test.svg( 工作正常)
通过网关应用程序:http://localhost:8080/img/test.svg http://localhost:8080/img/test.svg (GET http://localhost:8080/img/test.svg http://localhost:8080/img/test.svg404错误信息)
Update
找到了一种替代解决方案
例如,在网关应用程序中,您已将应用程序名称或应用程序上下文放置为“demo”
那么 Spring Boot 应用程序中的所有静态文件都需要位于“demo”目录下,即 src- main – resources – static -demo
我刚刚在这里记录了供任何人参考http://kotreshtm.blogspot.com/2018/10/serving-static-files-from-spring-boot.html http://kotreshtm.blogspot.com/2018/10/serving-static-files-from-spring-boot.html
如果有更好的解决方案/方法,请提出。
任何建议表示赞赏。
谢谢