我使用下面的方法來獲取從WAR文件中的資源WildFly獲得資源:WildFly - 從戰爭
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模塊加載有關,但我不知道這是一個錯誤還是正常行爲。
我在StackOverflow上發現了各種類似的問題,但沒有適用的解決方案。其中一個建議是使用ServletContext中,像這樣:
@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext = (ServletContext)this.wsContext.getMessageContext()
.get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);
但是,當我試圖獲得MessageContext中以這種方式,我得到一個IllegalStateException。所以我基本卡住了。有任何想法嗎?
? – UVM
從JAX-RS Web服務中的@GET方法。 –
更具體地說,從**無狀態** JAX-RS Web服務中的@GET方法。 –