我尝试通过具有RestEasy部署的Undertow服务器中的ResourceHandler
提供静态内容。
public class Server {
public static void main(String[] args) throws Exception {
UndertowJaxrsServer server = new UndertowJaxrsServer();
Undertow.Builder serverBuilder = Undertow
.builder()
.addHttpListener(8080, "0.0.0.0")
.setHandler(
Handlers.path().addPrefixPath(
"/web",
new ResourceHandler(new PathResourceManager(Paths.get("/some/fixed/path"),100))
.setDirectoryListingEnabled(true)
.addWelcomeFiles("index.html")));
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplicationClass(MyRestApplication.class.getName());
DeploymentInfo deploymentInfo = server.undertowDeployment(deployment, "/")
.setClassLoader(Server.class.getClassLoader())
.setContextPath("/api").setDeploymentName("WS");
server.deploy(deploymentInfo);
server.start(serverBuilder);
}
}
使用上面的代码,只有resteasy部署工作,我得到静态内容404(index.html)。
任何指针?谢谢!
答案 0 :(得分:0)
UndertowJaxrsServer API有点棘手。虽然您可以配置Undertow.Builder来启动服务器,但关联的处理程序将替换为默认的PathHandler实例,该实例也用于配置REST应用程序。
因此,添加更多HttpHandler(例如ResourceHandler)的正确方法是使用UndertowJaxrsServer#addResourcePrefixPath方法为您的请求指定额外的处理程序。
以下是使用上述API成功提供静态内容以及REST资源的示例:https://gist.github.com/sermojohn/928ee5f170cd74f0391a348b4a84fba0
答案 1 :(得分:0)
这是我的一个玩具项目:
public static void main(String[] args) {
UndertowJaxrsServer server = new UndertowJaxrsServer();
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplicationClass(RestEasyConfig.class.getName());
deployment.setInjectorFactoryClass(CdiInjectorFactory.class.getName());
DeploymentInfo deploymentInfo = server.undertowDeployment(deployment)
.setClassLoader(GatewayApi.class.getClassLoader())
.setContextPath("/api")
.addFilter(new FilterInfo("TokenFilter", TokenFilter.class))
.addFilterUrlMapping("TokenFilter", "/*", DispatcherType.REQUEST)
.addFilterUrlMapping("TokenFilter", "/*", DispatcherType.FORWARD)
.addListener(Servlets.listener(Listener.class))
.setDeploymentName("Undertow RestEasy Weld");
server.deploy(deploymentInfo);
server.addResourcePrefixPath("/index.htm", resource(new ClassPathResourceManager(GatewayApi.class.getClassLoader()))
.addWelcomeFiles("webapp/index.htm"));
Undertow.Builder undertowBuilder = Undertow.builder()
.addHttpListener(8080, "0.0.0.0");
server.start(undertowBuilder);
log.info(generateLogo());
}