问题描述
我使用以下方法从WildFly中的WAR文件获取资源:
this.getClass()。getResource (relativePath)
当应用程序部署为爆炸WAR时,它可以正常工作。 它曾经用于压缩WAR的工作。昨天,我在Eclipse中完成了项目的清理和重建,它刚刚停止工作。
当我检查资源根目录时:
logger.info(this.getClass()。getResource(/)。toExternalForm());
我明白了:
file:/ C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/
所以,难怪它不起作用。它可能与JBoss模块加载有关,但我不知道这是一个bug还是正常行为。
我在StackOverflow上发现了各种类似的问题,但是没有适用的解决方其中一个建议是像这样使用ServletContext:
@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext =(ServletContext)this.wsContext.getMessageContext()
.get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);
但是,当我尝试以这种方式获取MessageContext时,我收到IllegalStateException。所以我基本上卡住了。有什么想法吗?
我终于放弃了将资源文件放入新的JBoss模块中,如本链接所述。 / p>
它有效,但缺点是有两个部署目标,因此事情变得更复杂。从好的方面来说,WAR文件的大小会减少,如果只有部分资源发生了变化,我就不必重新部署应用程序。
I am using the following method to get a resource from WAR file in WildFly:
this.getClass().getResource(relativePath)
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());
I get this:
file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/
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.
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);
But, when I try to obtain MessageContext in this manner, I get an IllegalStateException. So I am basically stuck. Any ideas?
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
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获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!