我在Eclipse(Mars)中有一个带有嵌入式jetty v9的示例Maven项目。我为静态html / javascript文件定义了一个资源处理程序,为REST API定义了一个servlet处理程序。我创建了一个单个胖罐(阴影包),当我从项目homedir运行jar时,它会完成我想要的所有操作。它提供其余的API和网页。从任何其他目录,它只提供REST API。对于HTML页面,jetty服务器给出了找不到的原因。
处理程序以这种方式武装起来:
//REST handler context
ServletContextHandler restHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
restHandler.setContextPath("/");
// Jersey REST handling servlet
ServletHolder jerseyServlet = restHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tell Jersey which REST service class to load....
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", RestHandler.class.getCanonicalName());
// Handler that will handle the request for a given html page
ResourceHandler resourceHandler = new ResourceHandler();
// Configure page handler with login.html as the initial landing page
resourceHandler.setDirectoriesListed(true);
resourceHandler.setWelcomeFiles(new String[]{ "pages/login.html" });
resourceHandler.setResourceBase("src/main/webapp/");
//Add all handlers to the server
Server jettyServer = new Server(8080);
//ConstraintSecurityHandler authenticationHandler = authenticationService(jettyServer);
//authenticationHandler.setHandler( resourceHandler );
HandlerCollection handlerList = new HandlerCollection();
handlerList.setHandlers(new Handler[] { resourceHandler,,
restHandler,
new DefaultHandler(),
});
jettyServer.setHandler(handlerList);
POM.xml中的Build子句是这样的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.dastratum.DasConsole.ConsoleMain</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<minimizeJar>false</minimizeJar>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
<exclude>junit:junit</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
任何帮助表示赞赏。感谢。
答案 0 :(得分:1)
此问题的基本部分(混合ResourceHandler
&amp; ServletContextHandler
)之前已经得到解答。
然而,这里增加的皱纹是你正在使用Jersey,它(历史上)将默认为自己提供静态文件,不允许Servlet容器通过DefaultServlet提供静态文件。
建议您完全删除ResourceHandler
,并为ServletContextHandler
(其中包含Jersey)设置正确的ResourceBase,然后确保添加DefaultServlet
映射到/
。这可能工作得很好,泽西岛提供静态内容,但如果您需要更多控制静态文件服务(如缓存控制,高效内存使用,智能http / 2推送过滤器等),那么你将需要采取额外步骤配置Jersey以不提供静态内容,并让Jetty的DefaultServlet
完成工作。