getServletContext().getResourceAsStream() returns 空

getServletContext().getResourceAsStream() returns null

我正在使用 spring boot 2.0.1.RELEASE.

我正在将应用程序从 spring(WAR)迁移到 spring-boot。它使用 GWT,它使用依赖于 getServletContext().getResourseAsStream() 方法的 servlet 按预期工作。 我已经在 https://github.com/spring-projects/spring-boot/issues/12868.

上传了一个可复制的项目

当对 http://localhost:8080/mymodule/afile.rpc you can see the file is returned, but if you execute http://localhost:8080/mymodule/dispatch 执行一个 servlet 的 GET 请求时,servlet 使用 getServletContext().getResourseAsStream() 到 return 同一个文件,但是 null 是 returned案例.

有什么想法吗?

我之前看到过类似的问题并标记为已解决,但也许 spring 引导 2.0.1 再次失败。类似问题:#8525

根据Andy Wilkinson

Serving static resources from the classpath:static is a Spring MVC feature that is auto-configured by Spring Boot. The servlet container and servlet context know nothing about it is to be expected that the attempt to access the file via the servlet context does not work.

If you want static content to be served by the servlet container, you should place it in a location where the servlet container can find it. For example, in the root of an application packaged as a war file

我终于调整了 servlet 以使用类加载器而不是 servlet 上下文来选择文件。