我正在尝试使用Undertow来同时提供静态页面和Web服务(RestEasy),但无法同时使用它们。我看到其他人问同样的问题,如HTTP Handler and Resteasy Deployment with undertow and resteasy。必须有办法让这项工作,对吗? WildFly可以在同一个端口上处理这两个端口。但我怎么能这样做?谢谢!
答案 0 :(得分:0)
怎么样:
DeploymentInfo servletBuilder = deployment()
.setClassLoader(App.class.getClassLoader())
.setContextPath("/api")
.setDeploymentName("test.war")
.addServlets(
servlet("MessageServlet", MessageServlet.class)
.addInitParam("message", "Hello World")
.addMapping("/*"),
servlet("MyServlet", MessageServlet.class)
.addInitParam("message", "MyServlet")
.addMapping("/myservlet"));
DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
manager.deploy();
HttpHandler servletHandler = manager.start();
Undertow.builder().addHttpListener(8080, "0.0.0.0")
.setHandler(Handlers.path()
// REST API path
.addPrefixPath("/servlet", servletHandler)
// Serve all static files from a folder
.addPrefixPath("/static", new ResourceHandler(
new PathResourceManager(Paths.get("/path/to/www/"), 100))
.setWelcomeFiles("index.html"))
).build().start();
servlet部分取自Undertow的官方示例here。 您可以通过以下方式与Message servlet交谈:
GET /servlet/api
POST /servlet/api/myservlet