我正在迁移到 jetty 12,并且在 ResourceHandler 方面遇到了一些问题。我使用三个,一个用于 /robots.txt,一个用于 /sitemap.xml,一个用于 /files/ 中的所有其他文件。
我有两个问题:
我注意到 ResourceHandler 现在需要包装一个 ContextHandler,否则我会立即得到 nullPointerException。我也无法让 ResourceHandler#setBaseResourceAsString 完全工作,但在使用 ContextHandler#setBaseResourceAsString 时它似乎可以与 /files/ 一起工作。
有什么想法我做错了吗?
@Override
public void run() {
//...
// Configure jetty
jettyServer = new Server();
//...
// Calls all added handlers in list order
Handler.Sequence root = new Handler.Sequence();
jettyServer.setHandler(root);
root.addHandler(new GreeterHandler(this));
// Create a PathMappingsHandler to hold mappings
PathMappingsHandler mapHandler = new PathMappingsHandler();
root.addHandler(mapHandler);
// Add handlers to ContextHandlerCollection
// First, all dynamic handlers for user facing pages
mapHandler.addMapping(PathSpec.from(pageDescriptions.get(PAGE_INDEX).url()), new IndexHandler(this));
mapHandler.addMapping(PathSpec.from(pageDescriptions.get(PAGE_LOGIN).url()), new LoginHandler(this));
mapHandler.addMapping(PathSpec.from(pageDescriptions.get(PAGE_LOGOUT).url()), new LogoutHandler(this));
mapHandler.addMapping(PathSpec.from(pageDescriptions.get(PAGE_REGISTER).url()), new RegisterHandler(this);
mapHandler.addMapping(PathSpec.from("/robots.txt"), singleFileHandler("/robots.txt", "resources/private/robots.txt"));
mapHandler.addMapping(PathSpec.from("/sitemap.xml"), singleFileHandler("/sitemap.xml", "resources/private/sitemap.xml"));
// the rest of the /public folder
ResourceHandler publicFilesHandler = new ResourceHandler();
publicFilesHandler.setServer(jettyServer);
publicFilesHandler.setDirAllowed(true);
//publicFilesHandler.setUseFileMapping(true);
MimeTypes.Mutable types = new MimeTypes.Mutable();
types.addMimeMapping("js", "text/javascript; charset=utf-8");
types.addMimeMapping("css", "text/css; charset=utf-8");
types.addMimeMapping("svg", "image/svg+xml");
publicFilesHandler.setMimeTypes(types);
ContextHandler h = new ContextHandler(publicFilesHandler, "/files");
h.setBaseResourceAsString("resources/public/");
mapHandler.addMapping(PathSpec.from("/files/*"), h);
jettyServer.setRequestLog(new CustomRequestLog(new Slf4jRequestLogWriter(), CustomRequestLog.NCSA_FORMAT + " - %D"));
//logger.info(root.dump());
try {
jettyServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates a ResourceHandler that serves a single file.
*/
private Handler singleFileHandler(String contextPath, String filePath) {
ResourceHandler handler = new ResourceHandler();
handler.setServer(jettyServer);
ContextHandler result = new ContextHandler(handler);
result.setBaseResourceAsString(filePath);
return result;
}
编辑:我还尝试了文档中的以下最简单的设置,但我无法让它产生除 404 之外的任何内容:
ResourceHandler rh = new ResourceHandler();
rh.setBaseResource(ResourceFactory.of(rh).newResource("resources/public/"));
rh.setDirAllowed(true);
rh.setUseFileMapping(true);
rh.setServer(jettyServer);
mapHandler.addMapping(PathSpec.from("/files2/*"), rh);
A
ResourceHandler
以 Resource
作为基础。
该基址
Resource
必须存在,必须是一个目录,并且必须可由 ResourceHandler
读取。
当您调用
ResourceFactory.newResource("resources/public/")
时,如果未找到资源,该调用可能会导致 null。