问题描述
我使用以下方法从 WildFly 的 WAR 文件中获取资源:
I am using the following method to get a resource from WAR file in WildFly:
this.getClass().getResource(relativePath)
当应用程序部署为爆炸式 WAR 时,它可以工作.它曾经也适用于压缩的 WAR.昨天,我在 Eclipse 中对项目进行了清理和重建,但它刚刚停止工作.
It works when the application is deployed as exploded WAR. It used to work with compressed WAR, too. Yesterday, I did a clean and rebuild of project in Eclipse, and it just stopped working.
当我检查资源根目录时:
When I check the resource root:
logger.info(this.getClass().getResource("/").toExternalForm());
我明白了:
file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/
所以,难怪它不起作用.这可能与 JBoss 模块加载有关,但我不知道这是错误还是正常行为.
So, no wonder it doesn't work. It probably has something to do with JBoss module loading, but I don't know if this is a bug or normal behavior.
我在 StackOverflow 上发现了各种类似的问题,但没有适用的解决方案.建议之一是像这样使用 ServletContext:
I found various similar problems on StackOverflow, but no applicable solution. One of the suggestions is to use ServletContext like so:
@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext = (ServletContext)this.wsContext.getMessageContext()
.get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);
但是,当我尝试以这种方式获取 MessageContext 时,我得到了一个 IllegalStateException.所以我基本上被卡住了.有什么想法吗?
But, when I try to obtain MessageContext in this manner, I get an IllegalStateException. So I am basically stuck. Any ideas?
推荐答案
我终于放弃了,把我的资源文件放在一个新的 JBoss 模块中,如本链接所述.
I finally gave up and put my resource files in a new JBoss module, as described in this link.
https://community.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath
它有效,但缺点是有两个部署目标,所以事情更复杂.从好的方面来说,WAR 文件的大小减少了,如果只是部分资源发生了变化,我不必重新部署应用程序.
It works, but the downside is that there are two deployment targets so things are more complicated. On the upside, the size of the WAR file is reduced, and I don't have to redeploy the application if only some of the resources have changed.
这篇关于WildFly - 从 WAR 获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!