不要手动设置配置,这是第一个错误。
您的努力基本上破坏了配置,因为您不尊重原始默认配置或微妙的订单要求。
这是错误的方式,直接设置webapp配置。
webapp.setConfigurations(new Configuration[] { //
new AnnotationConfiguration(), // @WebServlet, @WebListener...
new WebXmlConfiguration(), // webapp/WEB-INF/web.xml
new WebInfConfiguration(), // ?
new MetaInfConfiguration(), // ?
new FragmentConfiguration(), // e.g. zkwebfragment-9.6.0.1.jar!/META-INF/web-fragment.xml
});
这是正确的方法,通过调整默认配置列表
在服务器级别。
Configuration.ClassList classlist = Configuration.ClassList
.setServerDefault(server);
classlist.addBefore(
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
这应该足够了。
您可以在服务器转储中看到默认配置顺序。
server.setDumpAfterStart(true);
server.start();
// and then on the output (see your logger) ...
| | | +> Configurations Servlet Dump WebApp@4c163e3 size=5
| | | | +> org.eclipse.jetty.webapp.WebInfConfiguration@5e403b4a
| | | | +> org.eclipse.jetty.webapp.WebXmlConfiguration@5117dd67
| | | | +> org.eclipse.jetty.webapp.MetaInfConfiguration@5be49b60
| | | | +> org.eclipse.jetty.webapp.FragmentConfiguration@2931522b
| | | | +> org.eclipse.jetty.webapp.JettyWebXmlConfiguration@7674b62c
| | | +> Handler attributes Servlet Dump WebApp@4c163e3 size=3
此转储功能很有用,因为它可能会告诉您 $HOME/.m2/repository jar 文件不存在于 webapp 自己的类路径中。
| | | +> WebAppClassLoader{Servlet Dump WebApp}@5fb759d6
| | | | +> URLs size=1
| | | | | +> file:/tmp/jetty-0_0_0_0-8080-ROOT_war-_-any-15598896298108484560/webapp/WEB-INF/classes/
如果您在此处没有看到您的 Maven 存储库文件,那么是时候配置您的 WebAppContext.setExtraClasspath(String) 以在启动 web 应用程序之前包含它们了。
另一个需要注意的功能是 2 个扫描正则表达式,用于识别哪些内容(类文件)被扫描。一个用于 webapp 类,一个用于服务器类。这两个都是相关的
用于发现 servlet 规范中的任何内容,无论是带注释的侦听器、带注释的 servlet、web 片段、servlet 容器初始化器、jsp 级别的详细信息、websocket 级别的详细信息等)
如果您想配置它们,它们都被设置为WebAppContext.setAttribute(String key, String value) 上的属性。
默认情况下,这些值为空,表示扫描所有 jar。
// This one controls server/container level discovery.
// An example of limited scanning would be
webappContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern"
".*/[^/]*servlet-api-[^/]*\\.jar$|.*[^/]*jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");
// This one controls webapp specific discovery.
webappContext.setAttribute(
"org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern"
".*/[^/]*foo-[^/]*\\.jar$|.*/[^/]*bar.*\\.jar$");
这方面的一个例子是......
你有一个带有...的网络应用程序
WEB-INF/lib/
foo-api-1.2.3.jar
foo-1.2.3.jar
bar-0.9.jar
boo-1.0.jar
并定义了 webapp 扫描...
webappContext.setAttribute(
"org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern",
".*/.*foo-api-[^/]\.jar$|./.*bar-[^/]\.jar$|./.*wibble[^/]*\.jar$");
然后将匹配并扫描以下文件:
WEB-INF/lib/
foo-api-1.2.3.jar
bar-0.9.jar