[英]Static resources from WebJars on IBM WebSphere Application Server not visible
I'm writing a simple reporting web application using JAX-RS and AngularJS deployed to IBM WebSphere Application Server. 我正在使用部署到IBM WebSphere Application Server的JAX-RS和AngularJS编写一个简单的报表Web应用程序。 Since it is really very simple I try to keep as little dependencies as possible, so I am not using any fancy framework server-side.
因为它非常简单,所以我尽量保持尽可能少的依赖关系,所以我没有使用任何花哨的框架服务器端。
For serving static resources I wrote this controller. 为了提供静态资源,我写了这个控制器。 I keep them in
src/main/resources
folder. 我将它们保存在
src/main/resources
文件夹中。
@Path("/")
public class Assets {
@GET
@Path("{path:.*\\.js}")
public Response javascript(@PathParam("path") String resourceName) {
return serveResource(resourceName, "application/javascript");
}
private Response serveResource(String resourceName, String contentType) {
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream(resourceName);
return Response.ok(inputStream)
.header(HttpHeaders.CONTENT_TYPE, contentType)
.build();
}
}
So far so good. 到现在为止还挺好。
As I use several JavaScript libraries (jQuery, Underscore.js, AngularJS), I wanted to make use of WebJars . 当我使用几个JavaScript库(jQuery,Underscore.js,AngularJS)时,我想使用WebJars 。 According to documentation on webjars.org:
根据文档上webjars.org:
With any Servlet 3 compatible container, the WebJars that are in the
WEB-INF/lib
directory are automatically made available as static resources.对于任何与Servlet 3兼容的容器,
WEB-INF/lib
目录中的WebJars将自动作为静态资源提供。 This works because anything in aMETA-INF/resources
directory in a JAR inWEB-INF/lib
is automatically exposed as a static resource.这是有效的,因为
WEB-INF/lib
JAR中的META-INF/resources
目录中的任何内容都会自动公开为静态资源。
When I put corresponding WebJars into my pom.xml
, things stopped working. 当我将相应的WebJars放入我的
pom.xml
,事情就停止了。 This is what I found out so far: 这是我到目前为止发现的:
.war
package, in WEB-INF/lib
folder. .war
包中,位于WEB-INF/lib
文件夹中。 ClassLoader
used in my serveResource
method claims in has all WebJars in its classpath. serveResource
方法声明中使用的ClassLoader
在其类路径中包含所有WebJars。 ClassLoader
's implementation is com.ibm.ws.classloader.CompoundClassLoader
. ClassLoader
的实现是com.ibm.ws.classloader.CompoundClassLoader
。 META-INF/resources
folder is visible to the ClassLoader
. META-INF/resources
文件夹是与可见ClassLoader
。 None. How can I make the class loader see the resources in the WebJars? 如何让类加载器看到WebJars中的资源?
For serving files form JAR META-INF/resources
you need WebSphere v8.5
. WebSphere v8.5
JAR META-INF/resources
提供文件,您需要WebSphere v8.5
。
Check this page for more details - Web container configuration for JavaServer Pages static file access 查看此页面以获取更多详细信息 - JavaServer Pages静态文件访问的Web容器配置
Use the following methods to locate your static files: 使用以下方法查找静态文件:
URL ServletContext.getResource(String path)
Set ServletContext.getResourcePaths(String path)
META-INF/resources directories of fragments under the WEB-INF/Lib directory in the application WAR file
应用程序WAR文件中WEB-INF / Lib目录下的片段的META-INF / resources目录
After searching pre-fragment document roots, the web container searches web fragments.
在搜索预片段文档根之后,Web容器搜索Web片段。 A web fragment comprises a JAR file in an application WEB-INF/lib directory.
Web片段包括应用程序WEB-INF / lib目录中的JAR文件。 The JAR might include static resources in a META-INF/resources directory that are defined within the JAR file.
JAR可能包含在JAR文件中定义的META-INF / resources目录中的静态资源。 To prevent the web container from searching META-INF/resources directories, set the
com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing
web container custom property to true.要阻止Web容器搜索META-INF / resources目录,请将
com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing
Web容器定制属性设置为true。 The default value for the custom property isfalse
.custom属性的默认值为
false
。com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing = true
UPDATE UPDATE
Try to change your method to something like this (pseudo code, no exception handling): 尝试将您的方法更改为此类(伪代码,无异常处理):
public Response javascript(@PathParam("path") String resourceName,
@Context ServletContext servletContext) {
// make sure resource name starts with / and should be relative path to META-INF/resources
URL url = serveltContext.getResource(resourceName);
InputStream inputStream = url.openStream();
return Response.ok(inputStream)
.header(HttpHeaders.CONTENT_TYPE, "application/javascript")
.build();
}
See ServletContext.getResource and URL.openStream 请参阅ServletContext.getResource和URL.openStream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.