我正在尝试运行HTTPServer和REST处理程序。一次只有一个人在工作,不能让它同时工作。我需要服务的html页面,也是api。
这是我的代码。
public class HttpServer {
private final UndertowJaxrsServer server = new UndertowJaxrsServer();
private static String rootPath = System.getProperty("user.dir");
private final Undertow.Builder serverBuilder;
public HttpServer(Integer port, String host) {
serverBuilder = Undertow
.builder()
.addHttpListener(port, host)
.setHandler(
Handlers.path().addPrefixPath(
"/",
Handlers.resource(
new FileResourceManager(new File(
rootPath + "/web"), 100))
.addWelcomeFiles(
rootPath + "/web/index.html")));
server.start(serverBuilder);
}
public DeploymentInfo deployApplication(String appPath,
Class<? extends Application> applicationClass) {
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplicationClass(applicationClass.getName());
return server.undertowDeployment(deployment, appPath);
}
public void deploy(DeploymentInfo deploymentInfo) throws ServletException {
server.deploy(deploymentInfo);
}
public static void main(String[] args) throws ServletException {
HttpServer myServer = new HttpServer(8080, "0.0.0.0");
DeploymentInfo di = myServer
.deployApplication("/rest", MyApplication.class)
.setClassLoader(HttpServer.class.getClassLoader())
.setContextPath("/my").setDeploymentName("My Application");
myServer.deploy(di);
}
}
UndertowJaxrsServer正在重写构建器的文件处理程序:
public UndertowJaxrsServer start(Undertow.Builder builder)
{
server = builder.setHandler(root).build();
server.start();
return this;
}
似乎没有办法将另一个处理程序传递给UndertowJaxrsServer。可能的解决方法是:
- 使用一个资源类部署另一个应用程序,该类只提供文件服务。
- 使用空白的Undertow,轻松轻松地部署JAX-RS。
从3.1.0版本。β 2及以上可以试试
UndertowJaxrsServer server = new UndertowJaxrsServer();
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplicationClass(ExampleApplication.class.getName());
DeploymentInfo deploymentInfo = server.undertowDeployment(deployment, "/");
deploymentInfo.setClassLoader(MyServer.class.getClassLoader());
deploymentInfo.setContextPath("/api");
server.deploy(deploymentInfo);
server.addResourcePrefixPath("/",
resource(new PathResourceManager(Paths.get(STATIC_PATH),100)).
addWelcomeFiles("index.html"));
server.start();
RestEasy应用程序将在/api/*和静态文件在/*